{{get_copyright()}}

/*!*********************************************************************************************************************
 * @file        Rte_{{appl_name}}.c
 * @brief       RTE implementation file
 * @details     Encapsulates the setup and management of tasks, events, and inter-component communication
 *              for an OS application
 * @date        {{get_generation_time_temp()}}
 * @toolversion {{get_vcos_version_temp()}}
 **********************************************************************************************************************/
{%- set gen_compu_method = get_gen_compu_method_flag() %}

{%- import 'macros.jinja2' as macros %}
{%- macro rte_write_complex_data(native_category, buffer_name, datatype_name) -%}
    {%- if native_category == 'STRUCTURE' -%}
    {{buffer_name}} = *(data);
    {%- else -%}
    Rte_MemCpy(&{{buffer_name}}, data, sizeof({{datatype_name}}));
    {%- endif %}
{%- endmacro %}

{%- macro rte_inter_ecu_lock_start(port, ind) %}
{%- if port["data_mapping_lock"] == "OSISR" %}
{{ind}}Rte_DisableOSInterruptsOS();
{%- elif port["data_mapping_lock"] == "SPINLOCK" %}
{{ind}}(void) GetSpinlock{{port["data_mapping_spinlock_api_postfix"]}}((SpinlockIdType) {{port["data_mapping_lock_id"]}});
{%- endif %}
{%- endmacro %}

{%- macro rte_inter_ecu_lock_end(port, ind) %}
{%- if port["data_mapping_lock"] == "OSISR" %}
{{ind}}Rte_EnableOSInterruptsOS();
{%- elif port["data_mapping_lock"] == "SPINLOCK" %}
{{ind}}(void) ReleaseSpinlock{{port["data_mapping_spinlock_api_postfix"]}}((SpinlockIdType) {{port["data_mapping_lock_id"]}});
{%- endif %}
{%- endmacro %}

{%- macro rte_write_inter_ecu_set_is_updated_flag(port, ind) %}
{%- if "is_updated" in port["data_mapping_flag"] %}
    {%- for flag in port["data_mapping_flag"]["is_updated"] %}
{{ind}}{{flag["parent_struct_name"]}}.{{flag["name"]}} = 1U;
    {%- endfor %}
{%- endif %}
{%- endmacro %}

{%- macro gen_tx_num_sig_dm_compu_method(ind, post, assigned, assign, data_sig_type, sig_cm_info, signal_name) %}
    {%- if sig_cm_info.get('category', None) == 'LINEAR' %}
{{ind}}{{data_sig_type}} local_{{assigned}};
{{ind}}local_{{assigned}} = ({{data_sig_type}})((float64)({{assign}}) * {{sig_cm_info['value']['coeff_final']}} + ({{sig_cm_info['value']['offset_final']}}));
{{ind}}ret{{post}}= com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, &local_{{assigned}});
    {%- elif sig_cm_info.get('category', None) in ['SCALE_LINEAR', 'SCALE_LINEAR_AND_TEXTTABLE'] %}
{{ind}}{{data_sig_type}} local_{{assigned}};
{{ind}}switch ((sint64)({{assign}}))
{{ind}}{
        {%- for sub in sig_cm_info['value'] %}
            {%- if sub['lower_limit'] == sub['upper_limit'] %}
{{ind}}    case ({{sub['lower_limit']}}):
{{ind}}        local_{{assigned}} = ({{data_sig_type}}){{sub['lower_limit']}};
{{ind}}        break;
            {%- else %}
{{ind}}    default:
{{ind}}        local_{{assigned}} = ({{data_sig_type}})((float64)({{assign}}) * {{sub['coeff_final']}} + ({{sub['offset_final']}}));
{{ind}}        break;
{{ind}}}
            {%- endif %}
        {%- endfor %}
{{ind}}ret{{post}}= com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, &local_{{assigned}});
    {%- else %}
{{ind}}ret{{post}}= com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, &{{assign}});
    {%- endif %}
{%- endmacro %}

{%- macro gen_rx_num_sig_dm_compu_method(ind, assigned, assign, data_sig_type, sig_cm_info) %}
    {%- if sig_cm_info.get('category', None) == 'LINEAR' %}
{{ind}}*{{assigned}} = ({{data_sig_type}})((float64)(*{{assign}}) * {{sig_cm_info['value']['coeff_final']}} + ({{sig_cm_info['value']['offset_final']}}));
    {%- elif sig_cm_info.get('category', None) in ['SCALE_LINEAR', 'SCALE_LINEAR_AND_TEXTTABLE'] %}
{{ind}}switch ((sint64)(*{{assign}}))
{{ind}}{
        {%- for sub in sig_cm_info['value'] %}
            {%- if sub['lower_limit'] == sub['upper_limit'] %}
{{ind}}    case ({{sub['lower_limit']}}):
{{ind}}        *{{assigned}} = ({{data_sig_type}}){{sub['lower_limit']}};
{{ind}}        break;
            {%- else %}
{{ind}}    default:
{{ind}}        *{{assigned}} = ({{data_sig_type}})((float64)(*{{assign}}) * {{sub['coeff_final']}} + ({{sub['offset_final']}}));
{{ind}}        break;
{{ind}}}
            {%- endif %}
        {%- endfor %}
    {%- endif %}
{%- endmacro %}

{%- macro gen_rx_signal_group_dm_compu_method(ind, struct, element, data_sig_type, sig_cm_info) %}
    {%- if sig_cm_info.get('category', None) == 'LINEAR' %}
{{ind}}{{struct}}->{{element}} = ({{data_sig_type}})((float64)({{struct}}->{{element}}) * {{sig_cm_info['value']['coeff_final']}} + ({{sig_cm_info['value']['offset_final']}}));
    {%- elif sig_cm_info.get('category', None) in ['SCALE_LINEAR', 'SCALE_LINEAR_AND_TEXTTABLE'] %}
{{ind}}switch ((sint64)({{struct}}->{{element}}))
{{ind}}{
        {%- for sub in sig_cm_info['value'] %}
            {%- if sub['lower_limit'] == sub['upper_limit'] %}
{{ind}}    case ({{sub['lower_limit']}}):
{{ind}}        {{struct}}->{{element}} = ({{data_sig_type}}){{sub['lower_limit']}};
{{ind}}        break;
            {%- else %}
{{ind}}    default:
{{ind}}        {{struct}}->{{element}} = ({{data_sig_type}})((float64)({{struct}}->{{element}}) * {{sub['coeff_final']}} + ({{sub['offset_final']}}));
{{ind}}        break;
{{ind}}}
            {%- endif %}
        {%- endfor %}
    {%- endif %}
{%- endmacro %}

{%- macro gen_tx_signal_group_dm_compu_method(input_buff, ind, post, ire, i_signal, data_sig_type, sig_cm_info) %}
    {%- if sig_cm_info.get('category', None) == 'LINEAR' %}
{{ind}}{{data_sig_type}} Local_{{ire}};
{{ind}}Local_{{ire}} = ({{data_sig_type}})((float64)({{input_buff}}->{{ire}}) * {{sig_cm_info['value']['coeff_final']}} + ({{sig_cm_info['value']['offset_final']}}));
{{ind}}ret{{post}}= com_send_signal(ComConf_ComGroupSignal_{{i_signal}}_Tx, &(Local_{{ire}}));
    {%- elif sig_cm_info.get('category', None) in ['SCALE_LINEAR', 'SCALE_LINEAR_AND_TEXTTABLE'] %}
{{ind}}{{data_sig_type}} Local_{{ire}};
{{ind}}switch ((sint64)({{input_buff}}->{{ire}}))
{{ind}}{
        {%- for sub in sig_cm_info['value'] %}
            {%- if sub['lower_limit'] == sub['upper_limit'] %}
{{ind}}    case ({{sub['lower_limit']}}):
{{ind}}        Local_{{ire}} = ({{data_sig_type}}){{sub['lower_limit']}};
{{ind}}        break;
            {%- else %}
{{ind}}    default:
{{ind}}        Local_{{ire}} = ({{data_sig_type}})((float64)({{input_buff}}->{{ire}}) * {{sub['coeff_final']}} + ({{sub['offset_final']}}));
{{ind}}        break;
{{ind}}}
            {%- endif %}
        {%- endfor %}
{{ind}}ret{{post}}= com_send_signal(ComConf_ComGroupSignal_{{i_signal}}_Tx, &(Local_{{ire}}));
    {%- else %}
{{ind}}ret{{post}}= com_send_signal(ComConf_ComGroupSignal_{{i_signal}}_Tx, &({{input_buff}}->{{ire}}));
    {%- endif %}
{%- endmacro %}

{%- macro rte_write_invoke_com_send(port, input_buff, rte_com_service_id, rte_com_api_id, ind) %}
{%- if port.get("signal_category", None) == "SignalGroup" %}
{{-rte_write_invoke_com_send_signal_group(port, input_buff, rte_com_service_id, rte_com_api_id, ind)}}
{%- else %}
{{-rte_write_invoke_com_send_signal(port, input_buff, rte_com_service_id, rte_com_api_id, ind)}}
{%- endif %}
{%- endmacro %}

{%- macro rte_write_invoke_com_send_signal(port, input_buff, rte_com_service_id, rte_com_api_id, ind) %}
{%- set and_op = "&" if port["arg"]["datatype"]["native_category"] == "NUMERICAL" else "" %}
{%- set signal_infos = port["data_mapping_details"] %}
#if (RTE_DLOG_ENABLED == STD_ON)
{%- if (port["mapping_to_signals"]|length) > 1 %}
{{ind}}uint8 ret_dlog;
{%- endif %}
{%- for signal_name in port["mapping_to_signals"] %}
    {%- if loop.first %}
        {%- if gen_compu_method and port["arg"]["datatype"]["native_category"] == "NUMERICAL"%}
            {%- for signal_info in signal_infos %}
                {%- if signal_info['full_name'] == signal_name %}
{{-gen_tx_num_sig_dm_compu_method(ind, " ", "data", input_buff, signal_info["real_sig_type"], signal_info['compu_method'], signal_name)}}
                {%- endif %}
            {%- endfor %}
        {%- else %}
{{ind}}ret = com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, {{and_op}}{{input_buff}});
        {%- endif %}
{{ind}}if (ret != E_OK)
{{ind}}{
{{ind}}    Rte_LogCom({{rte_com_service_id}}, {{rte_com_api_id}}, RTE_COM_SENDSIGNAL_ID, ComConf_ComSignal_{{signal_name}}_Tx, ret);
{{ind}}}
    {%- else %}
        {%- if gen_compu_method and port["arg"]["datatype"]["native_category"] == "NUMERICAL" %}
            {%- for signal_info in signal_infos %}
                {%- if signal_info['full_name'] == signal_name %}
{{-gen_tx_num_sig_dm_compu_method(ind, "_dlog ", "data", input_buff, signal_info["real_sig_type"], signal_info['compu_method'], signal_name)}}
                {%- endif %}
            {%- endfor %}
        {%- else %}
{{ind}}ret_dlog = com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, {{and_op}}{{input_buff}});
        {%- endif %}
{{ind}}if (ret_dlog != E_OK)
{{ind}}{
{{ind}}    Rte_LogCom({{rte_com_service_id}}, {{rte_com_api_id}}, RTE_COM_SENDSIGNAL_ID, ComConf_ComSignal_{{signal_name}}_Tx, ret);
{{ind}}}
{{ind}}ret |= ret_dlog;
    {%- endif %}
{%- endfor %}
#else
{%- for signal_name in port["mapping_to_signals"] %}
    {%- if loop.first %}
        {%- if gen_compu_method and port["arg"]["datatype"]["native_category"] == "NUMERICAL" %}
            {%- for signal_info in signal_infos %}
                {%- if signal_info['full_name'] == signal_name %}
{{-gen_tx_num_sig_dm_compu_method(ind, " ", "data", input_buff, signal_info["real_sig_type"], signal_info['compu_method'], signal_name)}}
                {%- endif %}
            {%- endfor %}
        {%- else %}
{{ind}}ret = com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, {{and_op}}{{input_buff}});
        {%- endif %}
    {%- else %}
        {%- if gen_compu_method and port["arg"]["datatype"]["native_category"] == "NUMERICAL" %}
            {%- for signal_info in signal_infos %}
                {%- if signal_info['full_name'] == signal_name %}
{{-gen_tx_num_sig_dm_compu_method(ind, " |", "data", input_buff, signal_info["real_sig_type"], signal_info['compu_method'], signal_name)}}
                {%- endif %}
            {%- endfor %}
        {%- else %}
{{ind}}ret |= com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, {{and_op}}{{input_buff}});
        {%- endif %}
    {%- endif %}
{%- endfor %}
#endif
{%- endmacro %}

{%- macro rte_write_invoke_com_send_signal_group(port, input_buff, rte_com_service_id, rte_com_api_id, ind) %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{ind}}uint8 ret_dlog;
{%- for sg_name in port["mapping_to_signals"] %}
    {%- if loop.first %}
        {%- for type_mapping in port["type_mappings"].get(sg_name) %}
            {%- if loop.first %}
                {%- if gen_compu_method %}
{{-gen_tx_signal_group_dm_compu_method(input_buff, ind, " ", type_mapping["implementation_record_element"], type_mapping["i_signal"], type_mapping["real_sig_type"], type_mapping["compu_method"])}}
                {%- else %}
{{ind}}ret = com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &({{input_buff}}->{{type_mapping["implementation_record_element"]}}));
                {%- endif %}
{{ind}}if (ret != E_OK)
{{ind}}{
{{ind}}    Rte_LogCom({{rte_com_service_id}}, {{rte_com_api_id}}, RTE_COM_SENDSIGNAL_ID, ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, ret);
{{ind}}}
            {%- else %}
                {%- if gen_compu_method %}
{{-gen_tx_signal_group_dm_compu_method(input_buff, ind, "_dlog ", type_mapping["implementation_record_element"], type_mapping["i_signal"], type_mapping["real_sig_type"], type_mapping["compu_method"])}}
                {%- else %}
{{ind}}ret_dlog = com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &({{input_buff}}->{{type_mapping["implementation_record_element"]}}));
                {%- endif %}
{{ind}}if (ret_dlog != E_OK)
{{ind}}{
{{ind}}    Rte_LogCom({{rte_com_service_id}}, {{rte_com_api_id}}, RTE_COM_SENDSIGNAL_ID, ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, ret);
{{ind}}}
{{ind}}ret |= ret_dlog;
            {%- endif %}
        {%- endfor %}
    {%- else %}
        {%- for type_mapping in port["type_mappings"].get(sg_name) %}
            {%- if gen_compu_method %}
{{-gen_tx_signal_group_dm_compu_method(input_buff, ind, "_dlog ", type_mapping["implementation_record_element"], type_mapping["i_signal"], type_mapping["real_sig_type"], type_mapping["compu_method"])}}
            {%- else %}
{{ind}}ret_dlog = com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &({{input_buff}}->{{type_mapping["implementation_record_element"]}}));
            {%- endif %}
{{ind}}if (ret_dlog != E_OK)
{{ind}}{
{{ind}}    Rte_LogCom({{rte_com_service_id}}, {{rte_com_api_id}}, RTE_COM_SENDSIGNAL_ID, ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, ret);
{{ind}}}
{{ind}}ret |= ret_dlog;
        {%- endfor %}
    {%- endif %}
{{ind}}ret_dlog = com_send_signal_group(ComConf_ComSignalGroup_{{sg_name}}_Tx);
{{ind}}if (ret_dlog != E_OK)
{{ind}}{
{{ind}}    Rte_LogCom({{rte_com_service_id}}, {{rte_com_api_id}}, RTE_COM_SENDSIGNALGROUP_ID, ComConf_ComSignalGroup_{{sg_name}}_Tx, ret);
{{ind}}}
{{ind}}ret |= ret_dlog;
{%- endfor %}
#else
{%- for sg_name in port["mapping_to_signals"] %}
    {%- if loop.first %}
        {%- for type_mapping in port["type_mappings"].get(sg_name) %}
            {%- if loop.first %}
                {%- if gen_compu_method %}
{{-gen_tx_signal_group_dm_compu_method(input_buff, ind, " ", type_mapping["implementation_record_element"], type_mapping["i_signal"], type_mapping["real_sig_type"], type_mapping["compu_method"])}}
                {%- else %}
{{ind}}ret = com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &({{input_buff}}->{{type_mapping["implementation_record_element"]}}));
                {%- endif %}
            {%- else %}
                {%- if gen_compu_method %}
{{-gen_tx_signal_group_dm_compu_method(input_buff, ind, " |", type_mapping["implementation_record_element"], type_mapping["i_signal"], type_mapping["real_sig_type"], type_mapping["compu_method"])}}
                {%- else %}
{{ind}}ret |= com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &({{input_buff}}->{{type_mapping["implementation_record_element"]}}));
                {%- endif %}
            {%- endif %}
        {%- endfor %}
    {%- else %}
        {%- for type_mapping in port["type_mappings"].get(sg_name) %}
            {%- if gen_compu_method %}
{{-gen_tx_signal_group_dm_compu_method(input_buff, ind, " |", type_mapping["implementation_record_element"], type_mapping["i_signal"], type_mapping["real_sig_type"], type_mapping["compu_method"])}}
            {%- else %}
{{ind}}ret |= com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &({{input_buff}}->{{type_mapping["implementation_record_element"]}}));
            {%- endif %}
        {%- endfor %}
    {%- endif %}
{{ind}}ret |= com_send_signal_group(ComConf_ComSignalGroup_{{sg_name}}_Tx);
{%- endfor %}
#endif
{%- endmacro %}

{%- macro rte_write_tx_com(pport, input_buff, rte_com_service_id, rte_com_api_id, ind) %}
{%- if pport["signal_category"] in ["Signal", "SignalGroup"] and pport["data_mapping_m_to_n"] != "0" %}
{%- set datatype = pport["arg"]["datatype"] %}
{%- set is_primitive = pport["arg"]["datatype"]["native_category"] == "NUMERICAL" %}
{%- set buff = pport["data_mapping_buff"] %}
{%- set buff_last = pport["data_mapping_buff_last"] %}
{%- if is_primitive %}
{#- primitive types #}
    {%- if pport["is_com_tx_opt"] %}
        {%- set is_proxy_sanding = pport["data_mapping_flag_is_proxy_sending"]["parent_struct_name"] + "." + pport["data_mapping_flag_is_proxy_sending"]["name"] %}
{{ind}}if ({{is_proxy_sanding}} || ({{buff_last["name"]}} != {{input_buff}}))
{{ind}}{
        {%- if pport["data_mapping_has_inter_core"] or pport["data_mapping_has_inter_osa_same_core"] %}
           {{-rte_inter_ecu_lock_start(pport, ind + "    ")}}
{{ind}}    {{buff_last["name"]}} = {{input_buff}};
{{ind}}    {{is_proxy_sanding}} = 0U;
{{ind}}    {{buff["name"]}} = {{input_buff}};
           {{-rte_write_inter_ecu_set_is_updated_flag(pport, ind + "    ")}}
           {{-rte_inter_ecu_lock_end(pport, ind + "    ")}}
            {%- if pport["data_mapping_status"] %}
{{ind}}    ret = {{pport["data_mapping_status"]["name"]}};
            {%- endif %}
        {%- else %}
           {{-rte_write_invoke_com_send(pport, input_buff, rte_com_service_id, rte_com_api_id, ind + "    ")}}
{{ind}}    if (ret == E_OK)
{{ind}}    {
{{ind}}        {{buff_last["name"]}} = {{input_buff}};
{{ind}}        {{is_proxy_sanding}} = 0U;
{{ind}}    }
        {%- endif %}
{{ind}}}
    {%- else %}
        {%- if pport["data_mapping_has_inter_core"] or pport["data_mapping_has_inter_osa_same_core"] %}
       {{-rte_inter_ecu_lock_start(pport, ind)}}
{{ind}}{{buff["name"]}} = {{input_buff}};
       {{-rte_write_inter_ecu_set_is_updated_flag(pport, ind)}}
       {{-rte_inter_ecu_lock_end(pport, ind)}}
            {%- if pport["data_mapping_status"] %}
{{ind}}ret = {{pport["data_mapping_status"]["name"]}};
            {%- endif %}
        {%- else %}
       {{-rte_write_invoke_com_send(pport, input_buff, rte_com_service_id, rte_com_api_id, ind)}}
        {%- endif %}
    {%- endif %}
{%- else %}
{#- complex types #}
    {%- if pport["is_com_tx_opt"] %}
        {%- set is_proxy_sanding = pport["data_mapping_flag_is_proxy_sending"]["parent_struct_name"] + "." + pport["data_mapping_flag_is_proxy_sending"]["name"] %}
{{ind}}if ({{is_proxy_sanding}} || (memcmp(&{{buff_last["name"]}}, {{input_buff}}, sizeof({{datatype["name"]}})) != 0))
{{ind}}{
        {%- if pport["data_mapping_has_inter_core"] or pport["data_mapping_has_inter_osa_same_core"] %}
           {{-rte_inter_ecu_lock_start(pport, ind + "    ")}}
{{ind}}    Rte_MemCpy(&{{buff_last["name"]}}, {{input_buff}}, sizeof({{datatype["name"]}}));
{{ind}}    {{is_proxy_sanding}} = 0U;
{{ind}}    Rte_MemCpy(&{{buff["name"]}}, {{input_buff}}, sizeof({{datatype["name"]}}));
           {{-rte_write_inter_ecu_set_is_updated_flag(pport, ind + "    ")}}
           {{-rte_inter_ecu_lock_end(pport, ind + "    ")}}
            {%- if pport["data_mapping_status"] %}
{{ind}}    ret = {{pport["data_mapping_status"]["name"]}};
            {%- endif %}
        {%- else %}
           {{-rte_write_invoke_com_send(pport, input_buff, rte_com_service_id, rte_com_api_id, ind + "    ")}}
{{ind}}    if (ret == E_OK)
{{ind}}    {
{{ind}}        Rte_MemCpy(&{{buff_last["name"]}}, {{input_buff}}, sizeof({{datatype["name"]}}));
{{ind}}        {{is_proxy_sanding}} = 0U;
{{ind}}    }
        {%- endif %}
{{ind}}}
    {%- else %}
        {%- if pport["data_mapping_has_inter_core"] or pport["data_mapping_has_inter_osa_same_core"] %}
       {{-rte_inter_ecu_lock_start(pport, ind)}}
{{ind}}Rte_MemCpy(&{{buff["name"]}}, {{input_buff}}, sizeof({{datatype["name"]}}));
       {{-rte_write_inter_ecu_set_is_updated_flag(pport, ind)}}
       {{-rte_inter_ecu_lock_end(pport, ind)}}
            {%- if pport["data_mapping_status"] %}
{{ind}}ret = {{pport["data_mapping_status"]["name"]}};
            {%- endif %}
        {%- else %}
       {{-rte_write_invoke_com_send(pport, input_buff, rte_com_service_id, rte_com_api_id, ind)}}
        {%- endif %}
    {%- endif %}
{%- endif %}
{%- endif %}
{%- endmacro %}

{%- macro rte_read_rx_com(rport, out_buff, is_out_buff_ptr, rte_com_service_id, rte_com_api_id, ind) %}
{%- if rport["signal_category"] in ["Signal", "SignalGroup"] and rport["data_mapping_m_to_n"] != "0" %}
{%- set datatype = rport["arg"]["datatype"] %}
{%- set out_buff_addr = out_buff if is_out_buff_ptr else ("&" + out_buff) %}
{%- set buff = rport["data_mapping_buff"] %}
{%- set is_updated_flag = rport["data_mapping_flag"].get("is_updated", {}) %}
{%- set signal_infos = rport["data_mapping_details"] %}
    {{-rte_inter_ecu_lock_start(rport, ind)}}
{%- if buff %}
    {%- if datatype["native_category"] == "NUMERICAL" %}
{{ind}}{{("*" + out_buff) if is_out_buff_ptr else out_buff}} = {{buff["name"]}};
    {%- elif datatype["native_category"] == "ARRAY" %}
{{ind}}Rte_MemCpy({{out_buff_addr}}, {{buff["name"]}}, sizeof({{datatype["name"]}}));
    {%- elif datatype["native_category"] == "STRUCTURE" %}
{{ind}}Rte_MemCpy({{out_buff_addr}}, &{{buff["name"]}}, sizeof({{datatype["name"]}}));
    {%- endif %}
    {%- if rport["data_mapping_status"] %}
{{ind}}ret = {{rport["data_mapping_status"]["name"]}};
    {%- endif %}
    {{-rte_inter_ecu_lock_end(rport, ind)}}
{%- else %}
    {#- directly receive from COM #}
    {%- set signal_name = rport.get("mapping_to_signals", [])[0] %}
    {%- if rport.get("signal_category", None) == "SignalGroup" %}
{{ind}}ret |= com_receive_signal_group(ComConf_ComSignalGroup_{{signal_name}}_Rx);
        {%- set type_mappings = rport["type_mappings"].get(signal_name)%}
        {%- for type_mapping in type_mappings %}
{{ind}}ret |= com_receive_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Rx, &({{out_buff_addr}}->{{type_mapping["implementation_record_element"]}}));
            {%- if gen_compu_method %}
{{-gen_rx_signal_group_dm_compu_method(ind, out_buff_addr, type_mapping["implementation_record_element"], type_mapping["datatype"], type_mapping['compu_method'])}}
            {%- endif %}
        {%- endfor %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{ind}}if (ret != E_OK)
{{ind}}{
{{ind}}    Rte_LogCom({{rte_com_service_id}}, {{rte_com_api_id}}, RTE_COM_RECEIVESIGNALGROUP_ID, ComConf_ComSignalGroup_{{signal_name}}_Rx, ret);
{{ind}}}
#endif
    {%- else %}
{{ind}}ret |= com_receive_signal(ComConf_ComSignal_{{signal_name}}_Rx, {{out_buff_addr}});
        {%- if gen_compu_method and datatype["native_category"] == "NUMERICAL" %}
{{-gen_rx_num_sig_dm_compu_method(ind, out_buff_addr, out_buff_addr, datatype['basetype'], signal_infos[0]['compu_method'])}}
        {%- endif %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{ind}}if (ret != E_OK)
{{ind}}{
{{ind}}    Rte_LogCom({{rte_com_service_id}}, {{rte_com_api_id}}, RTE_COM_RECEIVESIGNAL_ID, ComConf_ComSignal_{{signal_name}}_Rx, ret);
{{ind}}}
#endif
    {%- endif %}
{%- endif %}
{%- if is_updated_flag %}
{{ind}}{{is_updated_flag["parent_struct_name"]}}.{{is_updated_flag["name"]}} = 0U;
{%- endif %}
{%- endif %}
{%- endmacro %}

/*
 * DESCRIPTION
 * This source file is the implementation of Tasks behaviors and
 * some Rte interfaces.
 *
 * When tasks are activated, Basic task will excute its code directly,
 * while extended task will wait some events. Extended task will excute
 * codes corresponding to the specific event when getting the event.
 */
#define RTE_CORE
{%- for header_file in get_module_header_file_list() %}
#include "SchM_{{header_file}}.h"
{%- endfor %}
#include "Os.h"
#include "Rte.h"
#include "Rte_Cbk.h"
#include "Rte_Main.h"
#include "task_cfg.h"
#include "event_cfg.h"
#include "spinlock_cfg.h"
#include "schedtable_cfg.h"

{%- set swcs = get_appl_swc_name_list() %}
{%- for swc in swcs %}
#include "Rte_{{swc}}.h"
{%- endfor %}

#include "Rte_{{appl_name}}.h"
#include "Rte_Dlog.h"
#include "Rte_Hook.h"

{%- if "Com" in get_module_header_file_list() %}
#include "com.h"
{%- endif %}

{%- if "WdgM" in get_enable_module_list() %}
#include "wdgm.h"
{%- endif %}

{%- if wdgm_configured_in_isolate() and task_alive_configured_in_curr_appl() %}
#include "wdgm_ppcfunction.h"
{%- endif %}

{%- set core_id = get_appl_core_id(appl_name) %}
{%- set is_privileged = get_appl_is_privileged(appl_name) %}
{%- set isr_simplified = get_rte_generation_isr_simplified() %}
/*
 * API to control global interrupt enable/disable
 */
{%- if is_privileged and isr_simplified %}
#define Rte_DisableAllInterrupts() osDisableGlobalKM()
#define Rte_EnableAllInterrupts()  osEnableGlobalKM()
#define Rte_DisableOSInterrupts()  osDisableLevelKM()
#define Rte_EnableOSInterrupts()   osEnableLevelKM()
{%- else %}
#define Rte_DisableAllInterrupts() DisableAllInterrupts()
#define Rte_EnableAllInterrupts()  EnableAllInterrupts()
#define Rte_DisableOSInterrupts()  SuspendOSInterrupts()
#define Rte_EnableOSInterrupts()   ResumeOSInterrupts()
{%- endif %}

#define Rte_DisableAllInterruptsOS() DisableAllInterrupts()
#define Rte_EnableAllInterruptsOS()  EnableAllInterrupts()
#define Rte_DisableOSInterruptsOS()  SuspendOSInterrupts()
#define Rte_EnableOSInterruptsOS()   ResumeOSInterrupts()

#define RTE_TASK_TIMEOUT_EVENT_MASK   (1u)
#define RTE_TASK_WAITPOINT_EVENT_MASK (1u << 1)

#define RTE_MSEC_TO_TICKS(time_ms) ((uint32) ((((float32) time_ms) / (((float32) {{get_seconds_per_tick(appl_name)}}) * 1000.0F)) + 0.5F))
#define RTE_START_SCH_TBL_OFFSET (1U)


{#- SR defin dm compu method #}
{%- set compu_method_list = get_com_signal_compu_method_list(appl_name) %}
{%- if compu_method_list %}
/*
 * compu-method of text table/bit field text table
 */
{{-macros.define_compu_method(compu_method_list)}}
{%- endif %}

{#- SR non-zero init value template #}
{%- macro sr_init_value_non_zero(appl_prefix, appl_name_or_core_id, appl_postfix, buffer, is_extern="FALSE") %}
    {%- set datatype = buffer['arg']['datatype'] %}
    {%- if datatype['native_category'] == 'NUMERICAL' %}
        {%- set init_value = buffer["arg"].get("init_value", {}).get("value", {}) %}
        {%- set basetype = datatype["basetype"] %}
        {%- if basetype[0] == "b" and (init_value.get("value", "FALSE") == "TRUE" or init_value.get("value", "FALSE") == "True" or init_value.get("value", "FALSE") == "1")%}                    {#- boolean #}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}} = TRUE;
            {%- endif %}
        {%- elif basetype[0] == "u" and init_value.get("value", "0U") not in ["0U", "0ULL"] %}        {#- unsigned #}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}} = {{init_value.get("value", "0U")}};
            {%- endif %}
        {%- elif basetype[0] == "s" and init_value.get("value", "0") != "0" %}          {#- signed #}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}} = {{init_value.get("value", "0")}};
            {%- endif %}
        {%- elif basetype[0] == "f" and init_value.get("value", "0.0F") not in ["0.0", "0.0F"] %}    {#- float #}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}} = {{init_value.get("value", "0.0F")}};
            {%- endif %}
        {%- endif %}
    {%- else %}
        {%- set init_value_complex = buffer["arg"].get("init_value", {}).get("value", {}).get("value", {}) %}
        {%- set all_zero = all_complex_data_init_values_zero(init_value_complex) %}
        {%- if not all_zero %}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
            {%- set is_union = "TRUE" if (datatype['native_category'] == 'UNION') else 'FALSE' %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}} = {
    {{generate_complex_datatype_member(init_value_complex, is_union)}}
};
            {%- endif %}
        {%- endif %}
    {%- endif %}
{%- endmacro %}

{#- SR zero init value template #}
{%- macro sr_init_value_zero(appl_prefix, appl_name_or_core_id, appl_postfix, buffer, is_extern="FALSE") %}
    {%- set datatype = buffer['arg']['datatype'] %}
    {%- if datatype['native_category'] == 'NUMERICAL' %}
        {%- set init_value = buffer["arg"].get("init_value", {}).get("value", {}) %}
        {%- set basetype = datatype["basetype"] %}
        {%- if basetype[0] == "b" and not (init_value.get("value", "FALSE") == "TRUE" or init_value.get("value", "FALSE") == "True" or init_value.get("value", "FALSE") == "1")%}                    {#- boolean #}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- endif %}
        {%- elif basetype[0] == "u" and init_value.get("value", "0U") in ["0U", "0ULL"] %}             {#- unsigned #}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- endif %}
        {%- elif basetype[0] == "s" and init_value.get("value", "0") == "0" %}                          {#- signed #}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- endif %}
        {%- elif basetype[0] == "f" and init_value.get("value", "0.0F") in ["0.0", "0.0F"] %}           {#- float #}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- endif %}
        {%- endif %}
    {%- else %}
        {%- set init_value_complex = buffer["arg"].get("init_value", {}).get("value", {}).get("value", {}) %}
        {%- set all_zero = all_complex_data_init_values_zero(init_value_complex) %}
        {%- if all_zero %}
            {%- if is_extern == "TRUE" %}
extern VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- else %}
VAR({{datatype['name']}}, {{appl_prefix}}{{appl_name_or_core_id}}_{{appl_postfix}}) {{buffer["variable"]}};
            {%- endif %}
        {%- endif %}
    {%- endif %}
{%- endmacro %}

{#- Buffer extern for intra-osa S/R Non-Queue #}
{%- set buffers = get_sr_nonqueue_intra_osa_without_boolean_variable_dict_by_appl(appl_name) %}
{%- set zero_boolean_vars = get_sr_nonqueue_intra_osa_boolean_variable_dict_by_appl(appl_name, 'zero') -%}
{%- set nonzero_boolean_vars = get_sr_nonqueue_intra_osa_boolean_variable_dict_by_appl(appl_name, 'nonzero') -%}
{%- if buffers or zero_boolean_vars or nonzero_boolean_vars %}
{# blank line #}
/*
 * Buffer extern for intra-osa S/R Non-Queue
*/
{#- non-zero init value#}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- if nonzero_boolean_vars %}
extern VAR(Rte_Private_{{appl_name}}_NonZeroBool_VarType, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_Private_{{appl_name}}_NonZeroBool_Vars;
    {%- endif %}
{%- if buffers %}
    {%- for buffer in buffers[appl_name].values() %}
    {{-sr_init_value_non_zero("RTE_", appl_name.upper(), "PRIVATE_DATA", buffer, "TRUE")}}
    {%- endfor %}
{%- endif %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{#- zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- if zero_boolean_vars %}
extern VAR(Rte_Private_{{appl_name}}_ZeroBool_VarType, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_Private_{{appl_name}}_ZeroBool_Vars;
    {%- endif %}
    {%- if buffers %}
        {%- for buffer in buffers[appl_name].values() %}
{{-sr_init_value_zero("RTE_", appl_name.upper(), "PRIVATE_BSS", buffer, "TRUE")}}
        {%- endfor %}
    {%- endif %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
{%- endif %}

{#- Copy buffer extern for S/R Implicit #}
{%- set buffers = get_appl_sr_implicit_copy_buffer_list(appl_name) %}
{%- if buffers %}
{# blank line #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for task in buffers %}
extern VAR(Rte_ts_{{task}}, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_{{task}};
    {%- endfor%}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif %}

{#- Update flags extern for intra-osa S/R Receiver with enableUpdate == True #}
{%- set update_flags = get_all_sr_nonqueue_intra_osa_update_flag_dict_by_appl(appl_name) %}
{%- if update_flags %}
{# blank line #}
/*
 * Update flags extern for intra-osa S/R Receiver with enableUpdate == True
*/
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for flag in update_flags[appl_name] %}
extern VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_DATA) {{flag['flag_name']}};
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif%}

{%- set pports = get_all_mode_disabled_pports(appl_name) %}
{%- if pports %}
#define RTE_START_SEC_CODE
#include "rte_memmap.h"
    {%- for pport in pports %}
static FUNC(void, RTE_CODE) Rte_ActivateModeDisabling_{{pport["osapplication"]}}_{{pport["swc_name"]}}_{{pport["port_name"]}}_{{pport["mode_group_prototype"]}}(uint8 nextMode);
static FUNC(void, RTE_CODE) Rte_DeActivateModeDisabling_{{pport["osapplication"]}}_{{pport["swc_name"]}}_{{pport["port_name"]}}_{{pport["mode_group_prototype"]}}(uint8 prevMode);
    {%- endfor %}
#define RTE_STOP_SEC_CODE
#include "rte_memmap.h"
{%- endif %}

{#- Buffer extern for schedule table of basic task #}
{%- set task_list = get_cur_appl_basic_task_with_st_list() %}
{%- if task_list %}
{# blank line #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for task_info in task_list %}
extern VAR(uint32, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_ScheduleTable_{{task_info["task_name"]}}_Step;
    {%- endfor%}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif %}


{%- set appl_cs_multicore_p_ports = get_appl_cs_multicore_pports(appl_name) %}
{%- set appl_cs_multicore_r_ports = get_appl_cs_multicore_rports(appl_name) %}
{%- if appl_cs_multicore_p_ports or appl_cs_multicore_r_ports %}
{# blank line #}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"

{%- for pport in appl_cs_multicore_p_ports %}
    {%- set client_rports = pport["connected_ports"]["ports"] %}
    {%- for rport in client_rports %}
        {%- if rport["ioc"] %}
VAR(boolean, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_CS_Flag_{{pport["component"]}}_{{pport["name"]}}_{{rport["client_id"]}}_CallCompleted_Server;
VAR(Std_ReturnType, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_CS_Result_{{pport["component"]}}_{{pport["name"]}}_{{rport["client_id"]}};
        {%- endif %}
    {%- endfor %}
{%- endfor %}
{# blank line #}

{%- for rport in appl_cs_multicore_r_ports %}
    {%- if rport["ioc"] %}
        {%- set pport = rport["connected_ports"]["ports"][0] %}
VAR(boolean, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_CS_Flag_{{pport["component"]}}_{{pport["name"]}}_{{rport["client_id"]}}_CallCompleted_Client;
    {%- endif %}
{%- endfor %}

#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
{# blank line #}

{#- ---------------------------------------------------------------- #}
{#- cs arguments #}
{#- ---------------------------------------------------------------- #}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
{%- for rport in appl_cs_multicore_r_ports %}
    {%- set pport = rport["connected_ports"]["ports"][0] %}
    {%- for arg in rport["operation"]["args"].values() %}
        {%- if arg["direction"] == "IN" %}
VAR({{arg["type"]}}, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_CS_Arg_{{pport["component"]}}_{{pport["name"]}}_{{rport["client_id"]}}_{{arg["name"]}};
        {%- endif %}
    {%- endfor %}
{%- endfor %}
{# blank line #}

{%- for pport in appl_cs_multicore_p_ports %}
    {%- set client_rports = pport["connected_ports"]["ports"] %}
    {%- for rport in client_rports %}
        {%- for arg in pport["operation"]["args"].values() %}
            {%- if arg["direction"] == "OUT" %}
VAR({{arg["type"]}}, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_CS_Arg_{{pport["component"]}}_{{pport["name"]}}_{{rport["client_id"]}}_{{arg["name"]}};
            {%- endif %}
        {%- endfor %}
    {%- endfor %}
{%- endfor %}
{# blank line #}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
{%- endif %} {#- {%- if appl_cs_multicore_p_ports or appl_cs_multicore_r_ports %} #}

{%- if appl_cs_multicore_p_ports %}
{%- set ns = namespace(counter=0) %}
    {%- for pport in appl_cs_multicore_p_ports %}
        {%- set client_rports = pport["connected_ports"]["ports"] %}
        {%- for rport in client_rports %}
            {%- for arg in pport["operation"]["args"].values() %}
                {%- if arg["direction"] == "INOUT" and rport['infix']['mem_arg_inout'] == 'MULTI_APP'%}
                {%- set ns.counter = ns.counter + 1 %}
                    {%- if ns.counter == 1 %}
#define RTE_START_SEC_MULTI_APP_BSS
#include "rte_memmap.h"
                    {%- endif %}
VAR({{arg["type"]}}, RTE_MULTI_APP_BSS) Rte_CS_Arg_{{pport["component"]}}_{{pport["name"]}}_{{rport["client_id"]}}_{{arg["name"]}};
                {%- endif %}
            {%- endfor %}
        {%- endfor %}
    {%- endfor %}
    {%- if ns.counter > 0 %}
#define RTE_STOP_SEC_MULTI_APP_BSS
#include "rte_memmap.h"
    {%- endif %}
{%- endif %}

{%- if appl_cs_multicore_p_ports %}
{%- set ns = namespace(counter=0) %}
    {%- for pport in appl_cs_multicore_p_ports %}
        {%- set client_rports = pport["connected_ports"]["ports"] %}
        {%- set infix = appl_name + '_PRIVATE' %}
        {%- for rport in client_rports %}
            {%- for arg in pport["operation"]["args"].values() %}
                {%- if arg["direction"] == "INOUT" and rport['infix']['mem_arg_inout'] == infix %}
                {%- set ns.counter = ns.counter + 1 %}
                    {%- if ns.counter == 1 %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
                    {%- endif %}
VAR({{arg["type"]}}, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_CS_Arg_{{pport["component"]}}_{{pport["name"]}}_{{rport["client_id"]}}_{{arg["name"]}};
                {%- endif %}
            {%- endfor %}
        {%- endfor %}
    {%- endfor %}
    {%- if ns.counter > 0 %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- endif %}
{%- endif %}

{#- Buffer extern for S/R Non-IOC Queue #}
{%- set appl_queues = get_all_sr_queue_non_ioc_dequeue_dict_by_appl(appl_name) %}
{%- if appl_queues %}
{# blank line #}
    {%- set queues = appl_queues[appl_name] %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- for queue in queues %}
extern VAR({{queue['datatype']['name']}}, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_Q_{{queue['name']}}[{{queue['size']}}];
    {%- endfor %}
extern VAR(Rte_QRamInfoType, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_{{appl_name}}_QRamInfo[{{queues|length}}];
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"

#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
extern VAR(Rte_QOverflowType_{{appl_name}}, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_QOverflow_{{appl_name}};
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"

#define RTE_START_SEC_CONST
#include "rte_memmap.h"
extern CONST(Rte_QRomInfoType, RTE_CONST) Rte_{{appl_name}}_QRomInfo[{{queues|length}}];
#define RTE_STOP_SEC_CONST
#include "rte_memmap.h"

/*
 * Data structures for queue handling
 */
typedef uint8 Rte_QReturnType;
typedef uint8 Rte_QIndexType;

#define RTE_Q_OK     ((Rte_QReturnType)0)
#define RTE_Q_EMPTY  ((Rte_QReturnType)1)
#define RTE_Q_FULL   ((Rte_QReturnType)2)
    {%- set queues = appl_queues[appl_name] %}
    {%- set index = namespace(value = 0) %}
    {%- for queue in queues %}
#define RTE_Q_{{queue['name']}} ({{index.value}}U)
        {%- set index.value = index.value + 1 %}
    {%- endfor %}

#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- for queue in queues %}
VAR({{queue['datatype']['name']}}, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_Q_{{queue['name']}}[{{queue['size']}}];
    {%- endfor %}
VAR(Rte_QRamInfoType, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_{{appl_name}}_QRamInfo[{{queues|length}}];
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"

#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
VAR(Rte_QOverflowType_{{appl_name}}, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_QOverflow_{{appl_name}};
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"

#define RTE_START_SEC_CONST
#include "rte_memmap.h"
CONST(Rte_QRomInfoType, RTE_CONST) Rte_{{appl_name}}_QRomInfo[{{queues|length}}] = {
    {%- for queue in queues %}
    {
        (void*)&Rte_Q_{{queue['name']}}[0],
        sizeof({{queue['datatype']['name']}}),
        {{queue['size']}}
    },
    {%- endfor %}
};
#define RTE_STOP_SEC_CONST
#include "rte_memmap.h"

#define RTE_START_SEC_CODE
#include "rte_memmap.h"
FUNC(Rte_QReturnType, RTE_CODE) Rte_Enqueue_{{appl_name}}(Rte_QIndexType q, const void* data);
FUNC(Rte_QReturnType, RTE_CODE) Rte_Dequeue_{{appl_name}}(Rte_QIndexType q, void* data);
#define RTE_STOP_SEC_CODE
#include "rte_memmap.h"
{%- endif %}
{#- {%- if appl_queues %} #}

{#- ---------------------------------------------------------------- #}
{#- sr ioc queue vars #}
{#- ---------------------------------------------------------------- #}

{%- set sr_ioc_enqueues = get_all_sr_queue_ioc_1to1_1toN_enqueue_dict_by_appl(appl_name) %}
{%- set sr_ioc_dequeues = get_all_sr_queue_ioc_dequeue_dict_by_appl(appl_name) %}
{%- if sr_ioc_enqueues or sr_ioc_dequeues %}
/*
 * RTE internal IOC for queue handling
 */
{%- endif %}

{#- 1:1 / 1:n p-port #}
{%- if sr_ioc_enqueues %}
    {%- set ioc_enqueues = sr_ioc_enqueues[appl_name] %}
    {%- set queues_inter_osa_same_core = ioc_enqueues["inter_osa_same_core"] %}
    {%- set queues_inter_core = ioc_enqueues["inter_core"] %}
    {%- if queues_inter_osa_same_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}_Overflow0;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"

#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_tail;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- endif %}

    {%- if queues_inter_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}_Overflow0;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"

#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_tail;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- endif %}

    {%- if queues_inter_osa_same_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_osa_same_core %}
VAR({{queue['datatype']['name']}}, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}[{{queue['size']}}];
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
    {%- endif %}

    {%- if queues_inter_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_core %}
VAR({{queue['datatype']['name']}}, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}[{{queue['size']}}];
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
    {%- endif %}
{%- endif %}

{#- n:1 r-port #}
{%- if sr_ioc_dequeues %}
    {%- set ioc_dequeues = sr_ioc_dequeues[appl_name] %}
    {%- set queues_inter_osa_same_core = ioc_dequeues["inter_osa_same_core"] %}
    {%- set queues_inter_core = ioc_dequeues["inter_core"] %}
    {%- if queues_inter_osa_same_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}_Overflow1;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"

#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_head;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- endif %}

    {%- if queues_inter_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}_Overflow1;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"

#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
        {%- for queue in queues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_head;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- endif %}

#define RTE_START_SEC_MULTI_APP_BSS
#include "rte_memmap.h"
    {%- for queue in queues_inter_osa_same_core %}
VAR(uint8, RTE_MULTI_APP_BSS) Rte_ioc_{{queue['name']}}_ElementCnt;
    {%- endfor %}
    {%- for queue in queues_inter_core %}
VAR(uint8, RTE_MULTI_APP_BSS) Rte_ioc_{{queue['name']}}_ElementCnt;
    {%- endfor %}
#define RTE_STOP_SEC_MULTI_APP_BSS
#include "rte_memmap.h"
{%- endif %}

{#- ---------------------------------------------------------------- #}
{#- cs queue vars #}
{#- ---------------------------------------------------------------- #}

{%- set cs_ioc_enqueues = get_all_cs_ioc_1to1_enqueue_dict_by_appl(appl_name=appl_name) %}
{%- set cs_ioc_dequeues = get_all_cs_ioc_dequeue_dict_by_appl(appl_name=appl_name) %}
{%- if cs_ioc_enqueues or cs_ioc_dequeues %}
/*
 * RTE internal IOC for queue handling of C/S
 */
{%- endif %}
{#- cs 1:1 #}
{%- if cs_ioc_enqueues %}
    {#- enqueue: tail, overflow0, queue #}
    {%- for appl_name, queues in cs_ioc_enqueues.items() %}
        {%- set core_id = queues["core_id"] %}
        {%- set queues_intra_osa = queues["intra_osa"] %}
        {%- set queues_inter_osa_same_core = queues["inter_osa_same_core"] %}
        {%- set queues_inter_core = queues["inter_core"] %}
        {%- if queues_intra_osa or queues_inter_osa_same_core or queues_inter_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
            {%- for queue in queues_intra_osa %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_tail;
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_Overflow0;
            {%- endfor %}
            {%- for queue in queues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_tail;
            {%- endfor %}
            {%- for queue in queues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_tail;
            {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
        {%- endif %}
        {%- if queues_inter_osa_same_core or queues_inter_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
            {%- for queue in queues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}_Overflow0;
            {%- endfor %}
            {%- for queue in queues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}_Overflow0;
            {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- endif %}
        {%- if queues_intra_osa %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
            {%- for queue in queues_intra_osa %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}[{{queue['size']}}];
            {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
        {%- endif %}
        {%- if queues_inter_osa_same_core or queues_inter_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
            {%- for queue in queues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}[{{queue['size']}}];
            {%- endfor %}
            {%- for queue in queues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}[{{queue['size']}}];
            {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- endif %}
    {%- endfor %}
{%- endif %}

{#- cs server #}
{%- if cs_ioc_dequeues %}
    {#- dequeue: -var head, overflow1, elementcnt #}
    {%- set ioc_dequeues_intra_osa = cs_ioc_dequeues[appl_name]["intra_osa"] %}
    {%- set ioc_dequeues_inter_osa_same_core = cs_ioc_dequeues[appl_name]["inter_osa_same_core"] %}
    {%- set ioc_dequeues_inter_core = cs_ioc_dequeues[appl_name]["inter_core"] %}
    {%- if ioc_dequeues_intra_osa or ioc_dequeues_inter_osa_same_core or ioc_dequeues_inter_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- for queue in ioc_dequeues_intra_osa %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_head;
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_Overflow1;
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_ElementCnt;
    {%- endfor %}
    {%- for queue in ioc_dequeues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_head;
    {%- endfor %}
    {%- for queue in ioc_dequeues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_{{queue['name']}}_head;
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- endif %}

    {%- if ioc_dequeues_inter_osa_same_core or ioc_dequeues_inter_core %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
        {%- for queue in ioc_dequeues_inter_osa_same_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}_Overflow1;
        {%- endfor %}
        {%- for queue in ioc_dequeues_inter_core %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_{{queue['name']}}_Overflow1;
        {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
    {%- endif %}

    {%- if ioc_dequeues_inter_osa_same_core %}
#define RTE_START_SEC_MULTI_APP_BSS
#include "rte_memmap.h"
        {%- for queue in ioc_dequeues_inter_osa_same_core %}
VAR(uint8, RTE_MULTI_APP_BSS) Rte_ioc_{{queue['name']}}_ElementCnt;
        {%- endfor %}
#define RTE_STOP_SEC_MULTI_APP_BSS
#include "rte_memmap.h"
    {%- endif %}

    {%- if ioc_dequeues_inter_core %}
#define RTE_START_SEC_MULTI_APP_BSS
#include "rte_memmap.h"
        {%- for queue in ioc_dequeues_inter_core %}
VAR(uint8, RTE_MULTI_APP_BSS) Rte_ioc_{{queue['name']}}_ElementCnt;
        {%- endfor %}
#define RTE_STOP_SEC_MULTI_APP_BSS
#include "rte_memmap.h"
    {%- endif %}
{%- endif %}

{% set cs_multicore_rports = get_all_cs_multicore_rports() %}
{%- if cs_multicore_rports %}
/*
 * Client ID for C/S
 */
{%- endif %}
{%- for rport in cs_multicore_rports%}
#define Rte_CS_ClientConfigIndex_{{rport["component"]}}_{{rport["name"]}} {{rport["client_id"]}}
{%- endfor %}

{#- Buffer with initial value for S/R Non-Queue #}
{%- macro generate_complex_datatype_member(obj, is_union) %}
    {%- if obj is not mapping -%}
    {{obj}}     {# to the element of union is numerical #}
    {%- else -%}
        {%- if is_union == "TRUE" -%}
        {{"{"}}
        {%- endif %}
        {%- for sub in obj.values() %}
            {%- if sub is not mapping -%}
                {%- if not loop.last -%}
    {{sub}},
                {%- else -%}
    {{sub}}
                {%- endif -%}
            {%- else -%}
                {%- if not loop.last -%}
    {{"{"}}{{generate_complex_datatype_member(sub, "FALSE")}}{{"}"}},
                {%- else -%}
    {{"{"}}{{generate_complex_datatype_member(sub, "FALSE")}}{{"}"}}
                {%- endif -%}
            {%- endif %}
        {%- endfor %}
        {%- if is_union == "TRUE" -%}
        {{"}"}}
        {%- endif %}
    {%- endif %}
{%- endmacro %}

{%- set intra_osa_buffers = get_sr_nonqueue_intra_osa_without_boolean_variable_dict_by_appl(appl_name) -%}
{%- set zero_boolean_vars = get_sr_nonqueue_intra_osa_boolean_variable_dict_by_appl(appl_name, 'zero') -%}
{%- set nonzero_boolean_vars = get_sr_nonqueue_intra_osa_boolean_variable_dict_by_appl(appl_name, 'nonzero') -%}
{%- if intra_osa_buffers or zero_boolean_vars or nonzero_boolean_vars %}
/*
 * Buffers for intra-osa S/R Non-Queue
 */
{# non-zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- if nonzero_boolean_vars %}
    {%- set init_value_str = get_boolean_variables_init_value_list(appl_name, nonzero_boolean_vars) %}
VAR(Rte_Private_{{appl_name}}_NonZeroBool_VarType, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_Private_{{appl_name}}_NonZeroBool_Vars = {{init_value_str}};
    {%- endif %}
    {%- if intra_osa_buffers %}
        {%- for buffer in intra_osa_buffers[appl_name].values() %}
    {{-sr_init_value_non_zero("RTE_", appl_name.upper(), "PRIVATE_DATA", buffer)}}
        {%- endfor %}
    {%- endif %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{# zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- if zero_boolean_vars %}
VAR(Rte_Private_{{appl_name}}_ZeroBool_VarType, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_Private_{{appl_name}}_ZeroBool_Vars;
    {%- endif %}
    {%- if intra_osa_buffers %}
        {%- for buffer in intra_osa_buffers[appl_name].values() %}
    {{-sr_init_value_zero("RTE_", appl_name.upper(), "PRIVATE_BSS", buffer)}}
        {%- endfor %}
    {%- endif %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
{%- endif %}

{%- set osa_share_buffers = get_sr_nonqueue_osa_share_variable_dict_by_appl(appl_name) %}
{%- if osa_share_buffers %}
/*
 * Buffers for osa shared in same core S/R Non-Queue
 */
{# non-zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
{%- for buffer in osa_share_buffers[appl_name].values() %}
    {{-sr_init_value_non_zero("RTE_", appl_name.upper(), "SEMISHARED_DATA", buffer)}}
{%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
{# zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
{%- for buffer in osa_share_buffers[appl_name].values() %}
    {{-sr_init_value_zero("RTE_", appl_name.upper(), "SEMISHARED_BSS", buffer)}}
{%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
{%- endif %}

{%- set inter_osa_buffers = get_sr_nonqueue_core_share_variable_dict_by_appl(appl_name) %}
{%- if inter_osa_buffers %}
/*
 * Buffers for inter-osa in same core S/R Non-Queue
 */
{# non-zero init value #}
#define RTE_START_SEC_CORE{{core_id}}_SEMISHARED_DATA
#include "rte_memmap.h"
{%- for buffer in inter_osa_buffers[appl_name].values() %}
    {{-sr_init_value_non_zero("RTE_CORE", core_id, "SEMISHARED_DATA", buffer)}}
{%- endfor %}
#define RTE_STOP_SEC_CORE{{core_id}}_SEMISHARED_DATA
#include "rte_memmap.h"
{# zero init value #}
#define RTE_START_SEC_CORE{{core_id}}_SEMISHARED_BSS
#include "rte_memmap.h"
{%- for buffer in inter_osa_buffers[appl_name].values() %}
    {{-sr_init_value_zero("RTE_CORE", core_id, "SEMISHARED_BSS", buffer)}}
{%- endfor %}
#define RTE_STOP_SEC_CORE{{core_id}}_SEMISHARED_BSS
#include "rte_memmap.h"
{%- endif %}

{%- set inter_core_buffers = get_sr_nonqueue_global_variable_dict_by_appl(appl_name) %}
{%- set inter_core_buffers_appl = inter_core_buffers.get(appl_name, {}) %}
{%- set inter_core_buffers_global = inter_core_buffers.get("GLOBAL-VARS", {}) %}
{%- if inter_core_buffers %}
/*
 * Buffers for inter-core S/R Non-Queue
 */
{# non-zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
{%- for buffer in inter_core_buffers_appl.values() %}
    {{-sr_init_value_non_zero("RTE_", appl_name.upper(), "SEMISHARED_DATA", buffer)}}
{%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
{# zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
{%- for buffer in inter_core_buffers_appl.values() %}
    {{-sr_init_value_zero("RTE_", appl_name.upper(), "SEMISHARED_DATA", buffer)}}
{%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
{%- endif %}

{%- if inter_core_buffers_global %}
{# non-zero init value #}
#define RTE_START_SEC_MULTI_APP_DATA
#include "rte_memmap.h"
    {%- for buffer in inter_core_buffers_global.values() %}
        {{-sr_init_value_non_zero("RTE", "", "MULTI_APP_DATA", buffer)}}
    {%- endfor %}
#define RTE_STOP_SEC_MULTI_APP_DATA
#include "rte_memmap.h"
{# zero init value #}
#define RTE_START_SEC_MULTI_APP_DATA
#include "rte_memmap.h"
    {%- for buffer in inter_core_buffers_global.values() %}
        {{-sr_init_value_zero("RTE", "", "MULTI_APP_DATA", buffer)}}
    {%- endfor %}
#define RTE_STOP_SEC_MULTI_APP_DATA
#include "rte_memmap.h"
{%- endif %}

{%- set local_counters = get_appl_sr_unqueued_counters_definition(appl_name)["local"] %}
{%- if local_counters["intra_osa"] %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for local_counter in local_counters["intra_osa"] %}
VAR(uint32, RTE_{{appl_name.upper()}}_PRIVATE_DATA) {{local_counter}};
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif %}

{%- if local_counters["inter_osa_same_core"] %}
#define RTE_START_SEC_CORE{{core_id}}_SEMISHARED_DATA
#include "rte_memmap.h"
    {%- for local_counter in local_counters["inter_osa_same_core"] %}
VAR(uint32, RTE_CORE{{core_id}}_SEMISHARED_DATA) {{local_counter}};
    {%- endfor %}
#define RTE_STOP_SEC_CORE{{core_id}}_SEMISHARED_DATA
#include "rte_memmap.h"
{%- endif %}

{%- if local_counters["inter_core"] %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
    {%- for local_counter in local_counters["inter_core"] %}
VAR(uint32, RTE_{{appl_name.upper()}}_SEMISHARED_DATA) {{local_counter}};
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
{%- endif %}

{%- set global_counters = get_appl_sr_unqueued_counters_definition(appl_name)["global"] %}
{%- if global_counters["inter_core"] or global_counters["inter_osa_same_core"] %}
#define RTE_START_SEC_GLOBAL_DATA
#include "rte_memmap.h"
    {%- for global_counter in global_counters["inter_core"] + global_counters["inter_osa_same_core"] %}
VAR(uint32, RTE_GLOBAL_DATA) {{global_counter}};
    {%- endfor %}
#define RTE_STOP_SEC_GLOBAL_DATA
#include "rte_memmap.h"
{%- endif %}

{# Update flags for S/R Receiver with enableUpdate == True #}
{%- set intra_osa_flags = get_all_sr_nonqueue_intra_osa_update_flag_dict_by_appl(appl_name) %}
{%- set inter_osa_flags = get_all_sr_nonqueue_inter_osa_update_flag_dict_by_appl(appl_name) %}
{%- set inter_core_flags = get_all_sr_nonqueue_inter_core_update_flag_dict_by_appl(appl_name) %}
{%- if intra_osa_flags %}
/*
 * Update flags for intra-osa S/R Receiver with enableUpdate == True
 */
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for flag in intra_osa_flags[appl_name] %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_DATA) {{flag['flag_name']}};
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif %}

{%- if inter_osa_flags %}
/*
 * Update flags for inter-osa S/R Receiver with enableUpdate == True
 */
#define RTE_START_SEC_CORE{{core_id}}_SEMISHARED_DATA
#include "rte_memmap.h"
    {%- for flag in inter_osa_flags[appl_name] %}
VAR(uint8, RTE_CORE{{core_id}}_SEMISHARED_DATA) {{flag['flag_name']}};
    {%- endfor %}
#define RTE_STOP_SEC_CORE{{core_id}}_SEMISHARED_DATA
#include "rte_memmap.h"
{%- endif %}

{%- if inter_core_flags %}
/*
 * Update flags for inter-osa S/R Receiver with enableUpdate == True
 */
#define RTE_START_SEC_GLOBAL_DATA
#include "rte_memmap.h"
    {%- for flag in inter_core_flags[appl_name] %}
VAR(uint8, RTE_GLOBAL_DATA) {{flag['flag_name']}};
    {%- endfor %}
#define RTE_STOP_SEC_GLOBAL_DATA
#include "rte_memmap.h"
{%- endif %}

{# copy buffer for S/R Implicit #}
{%- set buffers = get_appl_sr_implicit_copy_buffer_list(appl_name) %}
{%- if buffers %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for task in buffers %}
WARNING_542_DISABLE
VAR(Rte_ts_{{task}}, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_{{task}};
WARNING_542_RESTORE
    {%- endfor%}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif %}

{# Buffer for schedule table of basic task #}
{%- set task_list = get_cur_appl_basic_task_with_st_list() %}
{%- if task_list %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for task_info in task_list %}
VAR(uint32, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_ScheduleTable_{{task_info["task_name"]}}_Step;
    {%- endfor%}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif %}

{#- ModeSwitchAck Flags #}
{%- set mode_switch_event_pports = get_appl_task_mode_switch_port_list(appl_name, with_pport = "yes") %}
{%- if mode_switch_event_pports|length %}
#define RTE_START_SEC_{{mode_switch_event_pports[0]['infix']['mem_ack_flags'].upper()}}_DATA
#include "rte_memmap.h"
/*
 * TxAck/ModeSwitchAck Flags
 */
VAR(Rte_{{appl_name}}_AckFlagsType, RTE_{{mode_switch_event_pports[0]['infix']['mem_ack_flags'].upper()}}_DATA) Rte_{{appl_name}}_AckFlags = {
{% for mode_switch_event_pport in mode_switch_event_pports %}
    {%- if not loop.last -%}
    1,
    {%- else -%}
    1
    {%- endif -%}
{%- endfor %}
};
#define RTE_STOP_SEC_{{mode_switch_event_pports[0]['infix']['mem_ack_flags'].upper()}}_DATA
#include "rte_memmap.h"

#define Rte_{{appl_name}}_AckFlagsInit() (Rte_MemClr(&Rte_{{appl_name}}_AckFlags, sizeof(Rte_{{appl_name}}_AckFlagsType)))

{%- endif %}
{#- RTE internal IOC replacements #}
{%- if mode_switch_event_pports|length %}
/*
 * RTE internal IOC replacements
 */
{#- shared zero init #}
    {%- set ns = namespace(counter=0) %}
    {%- for mode_switch_event_pport in mode_switch_event_pports %}
    {%- set swc_name = mode_switch_event_pport["swc_name"] %}
    {%- set port_name = mode_switch_event_pport["port_name"] %}
    {%- set mode_group_prototype_name = mode_switch_event_pport["mode_group_prototype"] %}
    {%- set queue_length = mode_switch_event_pport["queue_length"] %}
    {%- set inter_partition = mode_switch_event_pport["inter_partition"] %}
    {%- set connected_osapplication = mode_switch_event_pport["connected_osapplication"] %}
    {%- set osapplication = mode_switch_event_pport["osapplication"] %}
        {%- if inter_partition and connected_osapplication and connected_osapplication == appl_name %}
        {%- set ns.counter = ns.counter + 1 %}
            {%- if ns.counter == 1 %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
            {%- endif %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_DATA) Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head;
        {%- elif inter_partition and osapplication and osapplication == appl_name %}
        {%- set ns.counter = ns.counter + 1 %}
            {%- if ns.counter == 1 %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
            {%- endif %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_DATA) Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail;
        {%- else %}
            {#- do nothing #}
        {%- endif %}
    {%- endfor %}
    {%- if ns.counter > 0 %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_DATA
#include "rte_memmap.h"
    {%- endif %}
{#- shared no init #}
    {%- set ns = namespace(counter=0) %}
    {%- for mode_switch_event_pport in mode_switch_event_pports %}
    {%- set swc_name = mode_switch_event_pport["swc_name"] %}
    {%- set port_name = mode_switch_event_pport["port_name"] %}
    {%- set mode_group_prototype_name = mode_switch_event_pport["mode_group_prototype"] %}
    {%- set queue_length = mode_switch_event_pport["queue_length"] %}
    {%- set inter_partition = mode_switch_event_pport["inter_partition"] %}
    {%- set connected_osapplication = mode_switch_event_pport["connected_osapplication"] %}
    {%- set osapplication = mode_switch_event_pport["osapplication"] %}
        {%- if inter_partition and connected_osapplication and connected_osapplication == appl_name %}
            {#- do nothing #}
        {%- elif inter_partition and osapplication and osapplication == appl_name %}
        {%- set ns.counter = ns.counter + 1 %}
            {%- if ns.counter == 1 %}
#define RTE_START_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
            {%- endif %}
VAR(uint8, RTE_{{appl_name.upper()}}_SEMISHARED_BSS) Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[{{queue_length}}];
        {%- else %}
            {#- do nothing #}
        {%- endif %}
    {%- endfor %}
    {%- if ns.counter > 0 %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_SEMISHARED_BSS
#include "rte_memmap.h"
    {%- endif %}
{#- private no init #}
    {%- set ns = namespace(counter=0) %}
    {%- for mode_switch_event_pport in mode_switch_event_pports %}
    {%- set swc_name = mode_switch_event_pport["swc_name"] %}
    {%- set port_name = mode_switch_event_pport["port_name"] %}
    {%- set mode_group_prototype_name = mode_switch_event_pport["mode_group_prototype"] %}
    {%- set queue_length = mode_switch_event_pport["queue_length"] %}
    {%- set inter_partition = mode_switch_event_pport["inter_partition"] %}
    {%- set connected_osapplication = mode_switch_event_pport["connected_osapplication"] %}
    {%- set osapplication = mode_switch_event_pport["osapplication"] %}
        {%- if inter_partition and connected_osapplication and connected_osapplication == appl_name %}
            {#- do nothing #}
        {%- elif inter_partition and osapplication and osapplication == appl_name %}
            {#- do nothing #}
        {%- else %}
        {%- set ns.counter = ns.counter + 1 %}
            {%- if ns.counter == 1 %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
            {%- endif %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_BSS) Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[{{queue_length}}];
        {%- endif %}
    {%- endfor %}
    {%- if ns.counter > 0 %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- endif %}
{#- private zero init #}
    {%- set ns = namespace(counter=0) %}
    {%- for mode_switch_event_pport in mode_switch_event_pports %}
    {%- set swc_name = mode_switch_event_pport["swc_name"] %}
    {%- set port_name = mode_switch_event_pport["port_name"] %}
    {%- set mode_group_prototype_name = mode_switch_event_pport["mode_group_prototype"] %}
    {%- set queue_length = mode_switch_event_pport["queue_length"] %}
    {%- set inter_partition = mode_switch_event_pport["inter_partition"] %}
    {%- set connected_osapplication = mode_switch_event_pport["connected_osapplication"] %}
    {%- set osapplication = mode_switch_event_pport["osapplication"] %}
        {%- if inter_partition and connected_osapplication and connected_osapplication == appl_name %}
            {#- do nothing #}
        {%- elif inter_partition and osapplication and osapplication == appl_name %}
            {#- do nothing #}
        {%- else %}
        {%- set ns.counter = ns.counter + 1 %}
            {%- if ns.counter == 1 %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
            {%- endif %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail;
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head;
        {%- endif %}
    {%- endfor %}
    {%- if ns.counter > 0 %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- endif %}
{%- endif %}
{#- Data structures for mode management #}
{%- set mode_switch_pports = get_mode_switch_port_in_swc_list_for_modeuser(appl_name) %}
{%- if mode_switch_pports|length %}
/*
 * Data structure for mode management
 */
{#- intra partition #}
{%- set ns = namespace(counter=0) %}
    {%- for mode_switch_pport in mode_switch_pports %}
    {%- set swc_name = mode_switch_pport["swc_name"] %}
    {%- set port_name = mode_switch_pport["port_name"] %}
    {%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
    {%- set osapplication = mode_switch_pport["osapplication"] %}
    {%- set inter_partition = mode_switch_pport["inter_partition"] %}
        {%- if not inter_partition and osapplication == appl_name %}
        {%- set ns.counter = ns.counter + 1 %}
            {%- if ns.counter == 1 %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
            {%- endif %}
VAR(uint8, RTE_{{appl_name.upper()}}_PRIVATE_DATA) Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}} = RTE_MODE_{{mode_switch_pport["swc_name"]}}_{{mode_switch_pport["mode_group"]}}_{{mode_switch_pport["init_mode"]}};
        {%- endif %}
    {%- endfor %}
    {%- if ns.counter > 0 %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- endif %}
{#- inter partition #}
    {%- for mode_switch_pport in mode_switch_pports %}
    {%- set swc_name = mode_switch_pport["swc_name"] %}
    {%- set port_name = mode_switch_pport["port_name"] %}
    {%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
    {%- set inter_partition = mode_switch_pport["inter_partition"] %}
        {%- if inter_partition %}
#define RTE_START_SEC_{{mode_switch_pport['infix']['mem_mode_machine'].upper()}}_DATA
#include "rte_memmap.h"
VAR(uint8, RTE_{{mode_switch_pport['infix']['mem_mode_machine'].upper()}}_DATA) Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}} = RTE_MODE_{{mode_switch_pport["swc_name"]}}_{{mode_switch_pport["mode_group"]}}_{{mode_switch_pport["init_mode"]}};
#define RTE_STOP_SEC_{{mode_switch_pport['infix']['mem_mode_machine'].upper()}}_DATA
#include "rte_memmap.h"
        {%- endif %}
    {%- endfor %}
{%- endif %}
{%- set mode_switch_rports, mode_switch_pports = get_mode_switch_port_in_swc_list(appl_name) %}
{%- if mode_switch_pports|length %}
{%- for mode_switch_pport in mode_switch_pports %}
{%- set swc_name = mode_switch_pport["swc_name"] %}
{%- set port_name = mode_switch_pport["port_name"] %}
{%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
    {%- if mode_switch_pport["swc_mode_switch_event"]%}
{%- set transition_events = mode_switch_pport["swc_mode_switch_event"].get("transition_events", []) %}
{%- set entry_events = mode_switch_pport["swc_mode_switch_event"].get("entry_events", []) %}
{%- set exit_events = mode_switch_pport["swc_mode_switch_event"].get("exit_events", []) %}
    {%- for transition_event in get_transition_events_for_same_mode(transition_events) %}
#define RTE_MODE_TRANSITION_{{mode_switch_pport["swc_name"].upper()}}_{{mode_switch_pport["port_name"].upper()}}_{{mode_switch_pport["mode_group_prototype"].upper()}}_{{transition_event["previous_mode"]["name"].upper()}}_{{transition_event["next_mode"]["name"].upper()}} {{loop.index}}
    {%- endfor %}
    {%- if mode_switch_pport["on_transition_mode"] > 0 and transition_events|length %}
#define RTE_START_SEC_CONST
#include "rte_memmap.h"
CONST(uint8, RTE_CONST) Rte_ModeTransitionEventId_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}[{{mode_switch_pport["mode_group_len"]}}][{{mode_switch_pport["mode_group_len"]}}] =
{
    {%- for row in range(mode_switch_pport["mode_group_len"]) %}
    {
        {%- for col in range(mode_switch_pport["mode_group_len"]) %}
            {%- set is_matched = is_matched_transition_map(transition_events, row, col)[0] %}
            {%- set previous_mode_name = is_matched_transition_map(transition_events, row, col)[1] %}
            {%- set next_mode_name = is_matched_transition_map(transition_events, row, col)[2] %}
            {%- if is_matched %}
        RTE_MODE_TRANSITION_{{mode_switch_pport["swc_name"].upper()}}_{{mode_switch_pport["port_name"].upper()}}_{{mode_switch_pport["mode_group_prototype"].upper()}}_{{previous_mode_name.upper()}}_{{next_mode_name.upper()}}, /*{{ previous_mode_name}} -> {{next_mode_name }}*/
            {%- else %}
        (uint8)0U,
            {%- endif %}
        {%- endfor %}
    },
    {%- endfor %}
};
#define RTE_STOP_SEC_CONST
#include "rte_memmap.h"
    {%- endif %}
    {%- endif %}
{%- endfor %}
{%- endif %}

{%- set pports = get_all_mode_disabled_pports(appl_name) %}
{%- if pports %}
#define RTE_START_SEC_{{pports[0]['infix']['mem_mode_dis_trig'].upper()}}_DATA
#include "rte_memmap.h"
/*
 * RTE mode disabled entity flags
 */
VAR(Rte_Flag_ModeDisableTrigger_{{appl_name}}_Type, RTE_{{pports[0]['infix']['mem_mode_dis_trig'].upper()}}_DATA) Rte_Flag_ModeDisableTrigger_{{appl_name}} = {
{%- for pport in pports %}
    {%- for swc_run, entity_info in pport["mode_disabled_flag"].items() %}
        {%- if pport["init_mode"] in entity_info["disabled_mode"] %}
    TRUE,
        {%- else %}
    FALSE,
        {%- endif %}
    {%- endfor %}
{%- endfor %}
};
#define RTE_STOP_SEC_{{pports[0]['infix']['mem_mode_dis_trig'].upper()}}_DATA
#include "rte_memmap.h"

{%- endif %}

{#- RTE life cycle API #}
{%- set mode_switch_rports, mode_switch_pports = get_mode_switch_port_in_swc_list(appl_name) %}
{%- for mode_switch_pport in  mode_switch_pports %}
{%- set swc_name = mode_switch_pport["swc_name"] %}
{%- set port_name = mode_switch_pport["port_name"] %}
{%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
    {%- if mode_switch_pport["swc_mode_switch_event"]%}
        {%- set transition_events = mode_switch_pport["swc_mode_switch_event"].get("transition_events", []) %}
        {%- set entry_events = mode_switch_pport["swc_mode_switch_event"].get("entry_events", []) %}
        {%- set exit_events = mode_switch_pport["swc_mode_switch_event"].get("exit_events", []) %}
        {%- if exit_events | length %}
#define RTE_START_SEC_CODE
#include "rte_memmap.h"
EventMaskType Rte_GetModeExitEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 mode)
{
    EventMaskType eventMask;
    switch (mode)
    {
            {%- for mode, exit_event_ev_mask_list in  get_exit_events_ev_mask_list_for_same_mode(exit_events).items() %}
    case {{mode[0]}}U:
        eventMask = {{get_event_masks_with_type_conversion(exit_event_ev_mask_list)}};
        break;
            {%- endfor %}
    default:
        eventMask = (EventMaskType)0U;
        break;
    }
    return eventMask;

}
#define RTE_STOP_SEC_CODE
#include "rte_memmap.h"
        {%- endif %}
        {%- if entry_events | length %}
#define RTE_START_SEC_CODE
#include "rte_memmap.h"
EventMaskType Rte_GetModeEntryEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 mode)
{
    EventMaskType eventMask;
    switch (mode)
    {
            {%- for mode, entry_event_ev_mask_list in  get_entry_events_ev_mask_list_for_same_mode(entry_events).items() %}
    case {{mode[0]}}U:
        eventMask = {{get_event_masks_with_type_conversion(entry_event_ev_mask_list)}};
        break;
            {%- endfor %}
    default:
        eventMask = (EventMaskType)0U;
        break;
    }
    return eventMask;
}
#define RTE_STOP_SEC_CODE
#include "rte_memmap.h"
        {%- endif %}
        {%- if transition_events | length %}
#define RTE_START_SEC_CODE
#include "rte_memmap.h"
EventMaskType Rte_GetModeTransitionEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 transition)
{
    EventMaskType eventMask;
    switch (transition)
    {
            {%- for transition_mode, transition_event_ev_mask_list in  get_transition_event_ev_mask_list_for_same_mode(transition_events).items() %}
    case RTE_MODE_TRANSITION_{{mode_switch_pport["swc_name"].upper()}}_{{mode_switch_pport["port_name"].upper()}}_{{mode_switch_pport["mode_group_prototype"].upper()}}_{{transition_mode[0].upper()}}_{{transition_mode[1].upper()}}:
        eventMask = {{get_event_masks_with_type_conversion(transition_event_ev_mask_list)}};
        break;
            {%- endfor %}
    default:
        eventMask = (EventMaskType)0U;
        break;
    }
    return eventMask;
}
#define RTE_STOP_SEC_CODE
#include "rte_memmap.h"
        {%- endif %}
    {%- endif %}
{%- endfor %}

{#- ---------------------------------------------------------------- #}
{#- IRV buffers #}
{#- ---------------------------------------------------------------- #}

{%- set vars, vars_no_init_value = get_osa_irv_var(appl_name) %}
{%- set imp_copys = get_osa_irv_var_imp_copy(appl_name) %}
{%- if vars %}
{# non-zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for irv in vars %}
    {%- set init_value, is_zero, is_bool = get_sr_DATA_value(irv["datatype"], irv["init_value"]) %}
        {%- if not is_zero %}
VAR({{irv["datatype"]["name"]}}, RTE_{{appl_name.upper()}}_PRIVATE_DATA) {{irv["buff"]}} = {{init_value}};
        {%- endif %}
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{# zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for irv in vars %}
    {%- set init_value, is_zero, is_bool = get_sr_DATA_value(irv["datatype"], irv["init_value"]) %}
        {%- if is_zero %}
VAR({{irv["datatype"]["name"]}}, RTE_{{appl_name.upper()}}_PRIVATE_DATA) {{irv["buff"]}};
        {%- endif %}
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif %}

{%- if vars_no_init_value or imp_copys %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- for irv in vars_no_init_value %}
VAR({{irv["datatype"]["name"]}}, RTE_{{appl_name.upper()}}_PRIVATE_BSS) {{irv["buff"]}};
    {%- endfor %}
    {%- for access in imp_copys %}
VAR({{access["data"]["datatype"]["name"]}}, RTE_{{appl_name.upper()}}_PRIVATE_BSS) {{access["buff_imp_copy"]}};
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
{%- endif %}

{#- ---------------------------------------------------------------- #}
{#- PIM buffers #}
{#- ---------------------------------------------------------------- #}

{%- set vars, vars_no_init_value = get_osa_pim_var(appl_name) %}
{%- if vars %}
{# non-zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for pim in vars %}
    {%- set init_value, is_zero, is_bool = get_sr_DATA_value(pim["datatype"], pim["init_value"]) %}
        {%- if not is_zero %}
VAR({{pim["buff_type"]}}, RTE_{{appl_name.upper()}}_PRIVATE_DATA) {{pim["buff"]}} = {{init_value}};
        {%- endif %}
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{# zero init value #}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
    {%- for pim in vars %}
    {%- set init_value, is_zero, is_bool = get_sr_DATA_value(pim["datatype"], pim["init_value"]) %}
        {%- if is_zero %}
VAR({{pim["buff_type"]}}, RTE_{{appl_name.upper()}}_PRIVATE_DATA) {{pim["buff"]}};
        {%- endif %}
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_DATA
#include "rte_memmap.h"
{%- endif %}
{%- if vars_no_init_value %}
#define RTE_START_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
    {%- for pim in vars_no_init_value %}
VAR({{pim["buff_type"]}}, RTE_{{appl_name.upper()}}_PRIVATE_BSS) {{pim["buff"]}};
    {%- endfor %}
#define RTE_STOP_SEC_{{appl_name.upper()}}_PRIVATE_BSS
#include "rte_memmap.h"
{%- endif %}

{#- ---------------------------------------------------------------- #}
{#- data mapping global variables #}
{#- ---------------------------------------------------------------- #}

{#- data buffers #}
{%- for infix, vars in get_sr_dm_buff(appl_name).items() %}
{%- set infix_upper = infix.upper() %}
#define RTE_START_SEC_{{infix_upper}}
#include "rte_memmap.h"
    {%- for var in vars %}
        {%- if var["init_value_all_zero"] %}
VAR({{var["datatype"]["name"]}}, RTE_{{infix_upper}}) {{var["name"]}};
        {%- else %}
VAR({{var["datatype"]["name"]}}, RTE_{{infix_upper}}) {{var["name"]}} = {{var["init_value_str"]}};
        {%- endif %}
    {%- endfor %}
#define RTE_STOP_SEC_{{infix_upper}}
#include "rte_memmap.h"
{%- endfor %}

{%- for infix, vars in get_sr_dm_buff_last(appl_name).items() %}
{%- set infix_upper = infix.upper() %}
#define RTE_START_SEC_{{infix_upper}}
#include "rte_memmap.h"
    {%- for var in vars %}
VAR({{var["datatype"]["name"]}}, RTE_{{infix_upper}}) {{var["name"]}};
    {%- endfor %}
#define RTE_STOP_SEC_{{infix_upper}}
#include "rte_memmap.h"
{%- endfor %}

{%- for infix, vars in get_sr_dm_buff_status(appl_name).items() %}
{%- set infix_upper = infix.upper() %}
#define RTE_START_SEC_{{infix_upper}}
#include "rte_memmap.h"
    {%- for var in vars %}
VAR(uint8, RTE_{{infix_upper}}) {{var["name"]}};
    {%- endfor %}
#define RTE_STOP_SEC_{{infix_upper}}
#include "rte_memmap.h"
{%- endfor %}

{#- flags #}
{%- macro sr_dm_flag_def(parent_struct_name, infix, init) %}
{%- set infix_upper = infix.upper() %}
#define RTE_START_SEC_{{infix_upper}}
#include "rte_memmap.h"
VAR({{parent_struct_name}}_Type, RTE_{{infix_upper}}) {{parent_struct_name}}{{init}};
#define RTE_STOP_SEC_{{infix_upper}}
#include "rte_memmap.h"
{%- endmacro %}

{%- for parent_struct_name, vars in get_sr_dm_flag_is_proxy_sending(appl_name).items() %}
{{-sr_dm_flag_def(parent_struct_name, vars[0], vars[2])}}
{%- endfor %}

{%- for parent_struct_name, vars in get_sr_dm_flag_is_updated_tx(appl_name).items() %}
{{-sr_dm_flag_def(parent_struct_name, vars[0], "")}}
{%- endfor %}

{%- for parent_struct_name, vars in get_sr_dm_flag_is_updated_rx(appl_name).items() %}
{{-sr_dm_flag_def(parent_struct_name, vars[0], "")}}
{%- endfor %}

{%- for parent_struct_name, vars in get_sr_dm_flag_is_received(appl_name).items() %}
{{-sr_dm_flag_def(parent_struct_name, vars[0], "")}}
{%- endfor %}

{%- for parent_struct_name, vars in get_sr_dm_flag_is_timeout(appl_name).items() %}
{{-sr_dm_flag_def(parent_struct_name, vars[0], "")}}
{%- endfor %}

{#- ---------------------------------------------------------------- #}
{#- MODE functions #}
{#- ---------------------------------------------------------------- #}

{%- set mode_switch_pport_used_explicit = get_mode_switch_pport_use_explicit_order_in_swc_list() %}
{%- if mode_switch_pport_used_explicit %}
#define RTE_START_SEC_CODE
#include "rte_memmap.h"
/**
 * Helper functions for mode management
 */
    {%- for swc_mode_group_pair, mode_decl in mode_switch_pport_used_explicit.items() %}
FUNC(uint8, RTE_CODE) Rte_GetInternalModeIndex_{{swc_mode_group_pair[0]}}_{{swc_mode_group_pair[1]}}(uint8 mode);
    {%- endfor %}
#define RTE_STOP_SEC_CODE
#include "rte_memmap.h"
{%- endif %}

#define RTE_START_SEC_CODE
#include "rte_memmap.h"

{#- RTE internal IOC replacements #}
{%- if mode_switch_event_pports|length %}
/*
 * RTE internal IOC replacements
 */
{%- for mode_switch_event_pport in mode_switch_event_pports %}
{%- set swc_name = mode_switch_event_pport["swc_name"] %}
{%- set port_name = mode_switch_event_pport["port_name"] %}
{%- set mode_group_prototype_name = mode_switch_event_pport["mode_group_prototype"] %}
{%- set queue_length = mode_switch_event_pport["queue_length"] %}
{%- set inter_partition = mode_switch_event_pport["inter_partition"] %}
{%- set connected_osapplication = mode_switch_event_pport["connected_osapplication"] %}
{%- set osapplication = mode_switch_event_pport["osapplication"] %}

{%- if inter_partition and connected_osapplication and connected_osapplication == appl_name %}
    {%- if queue_length == 1 %}
Std_ReturnType Rte_IccReceive_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0);
Std_ReturnType Rte_IccReceive_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0)
{
    Std_ReturnType result = RTE_E_OK;
    if (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail == Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head)
    {
        result = RTE_E_NO_DATA;
    }
    else
    {
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head = !Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head;
        *data0 = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[0];
        result = RTE_E_OK;
    }
    return result;
}
    {%- elif queue_length > 1 %}
Std_ReturnType Rte_IccReceive_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0);
Std_ReturnType Rte_IccReceive_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0)
{
    Std_ReturnType result;
    if (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head == Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail)
    {
        result = RTE_E_NO_DATA;
    }
    else
    {
        uint8_least head = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head;
        if (head < ((2U * {{queue_length}}U) - 1U))
        {
            ++head;
        }
        else
        {
            head = 0U;
        }
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head = (uint8)head;
        if (head >= {{queue_length}}U)
        {
            head -= {{queue_length}}U;
        }

        *data0 = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[head];
        result = RTE_E_OK;
    }

    return result;
}
Std_ReturnType Rte_IccPeek_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0);
Std_ReturnType Rte_IccPeek_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0)
{
    Std_ReturnType ret = RTE_E_NO_DATA;
    if (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head != Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail)
    {
        uint8_least head = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head;
        if (head < ((2U * {{queue_length}}U) - 1U))
        {
            ++head;
        }
        else
        {
            head = 0U;
        }
        if (head >= {{queue_length}}U)
        {
            head -= {{queue_length}}U;
        }

        *data0 = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[head];
        ret = RTE_E_OK;
    }

    return ret;
}
    {%- endif %}
{%- elif inter_partition and osapplication and osapplication == appl_name %}
    {%- if queue_length == 1 %}
Std_ReturnType Rte_IccSend_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(const uint8 data0);
Std_ReturnType Rte_IccSend_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(const uint8 data0)
{
    Std_ReturnType result = RTE_E_OK;
    if (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head != Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail)
    {
        result = RTE_E_LIMIT;
    }
    else
    {
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail = !Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail;
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[0] = data0;
    }
  return result;
}
    {%- elif queue_length > 1 %}
Std_ReturnType Rte_IccSend_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(const uint8 data0);
Std_ReturnType Rte_IccSend_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(const uint8 data0)
{
    Std_ReturnType result = RTE_E_OK;
    uint8 queueSize = (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head > Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail) ? ((Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head - Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail)) : (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail - Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head);
    if ( queueSize == {{queue_length}}U)
    {
        result = RTE_E_LIMIT;
    }
    else
    {
        uint8_least tail = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail;
        if (tail < ((2U * {{queue_length}}U) - 1U))
        {
            ++tail;
        }
        else
        {
            tail = 0U;
        }
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail = (uint8)tail;
        if (tail >= {{queue_length}}U)
        {
            tail -= {{queue_length}}U;
        }

        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[tail] = data0;
    }

    return result;
}
    {%- endif %}
{%- else %}
    {%- if queue_length == 1 %}
Std_ReturnType Rte_IccSend_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(const uint8 data0)
{
    Std_ReturnType result = RTE_E_OK;
    if (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head != Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail)
    {
        result = RTE_E_LIMIT;
    }
    else
    {
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail = !Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail;
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[0] = data0;
    }
  return result;
}
Std_ReturnType Rte_IccReceive_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0)
{
    Std_ReturnType result = RTE_E_OK;
    if (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail == Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head)
    {
        result = RTE_E_NO_DATA;
    }
    else
    {
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head = !Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head;
        *data0 = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[0];
        result = RTE_E_OK;
    }
    return result;
}
    {%- elif queue_length > 1 %}
Std_ReturnType Rte_IccSend_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(const uint8 data0)
{
    Std_ReturnType result = RTE_E_OK;
    uint8 queueSize = (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head > Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail) ? ((Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head - Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail)) : (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail - Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head);
    if ( queueSize == {{queue_length}}U)
    {
        result = RTE_E_LIMIT;
    }
    else
    {
        uint8_least tail = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail;
        if (tail < ((2U * {{queue_length}}U) - 1U))
        {
            ++tail;
        }
        else
        {
            tail = 0U;
        }
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail = (uint8)tail;
        if (tail >= {{queue_length}}U)
        {
            tail -= {{queue_length}}U;
        }

        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[tail] = data0;
    }

    return result;
}
Std_ReturnType Rte_IccReceive_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0)
{
    Std_ReturnType result;
    if (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head == Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail)
    {
        result = RTE_E_NO_DATA;
    }
    else
    {
        uint8_least head = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head;
        if (head < ((2U * {{queue_length}}U) - 1U))
        {
            ++head;
        }
        else
        {
            head = 0U;
        }
        Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head = (uint8)head;
        if (head >= {{queue_length}}U)
        {
            head -= {{queue_length}}U;
        }

        *data0 = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[head];
        result = RTE_E_OK;
    }

    return result;
}
Std_ReturnType Rte_IccPeek_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 *data0)
{
    Std_ReturnType ret = RTE_E_NO_DATA;
    if (Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head != Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail)
    {
        uint8_least head = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head;
        if (head < ((2U * {{queue_length}}U) - 1U))
        {
            ++head;
        }
        else
        {
            head = 0U;
        }
        if (head >= {{queue_length}}U)
        {
            head -= {{queue_length}}U;
        }

        *data0 = Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[head];
        ret = RTE_E_OK;
    }

    return ret;
}
    {%- endif %}
{%- endif %}
{%- endfor %}
{%- endif %}
{#- Implementation for Mode reading API (Rte_Mode) -#}
{%- if mode_switch_rports|length %}
{#- Rte_Mode API prototypes #}
/*
 * Mode reading API (Rte_Mode)
 */
    {%- for mode_switch_rport in mode_switch_rports %}
    {%- set connected_osapplication = mode_switch_rport["connected_osapplication"] %}
    {%- set inter_partition = mode_switch_rport["inter_partition"] %}
        {%- if mode_switch_rport["connected_component_name"] %}
            {%- if mode_switch_rport["use_enhanced_mode_api"] %}
FUNC(uint8,RTE_CODE) Rte_Mode_{{mode_switch_rport["swc_name"]}}_{{mode_switch_rport["port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}}(uint8 *previousMode, uint8 *nextMode)
{
    uint8 curMode;
    Rte_DisableOSInterrupts();
                {%- if mode_switch_rport["swc_mode_switch_event"] and mode_switch_rport.get("conn_asw_ports", [{}])[0].get("queue_length", 0) > 1 %}
    if (Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{mode_switch_rport["connected_component_name"]}}_{{mode_switch_rport["connect_port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}}_Ack {{"== 0U" if not inter_partition else '!= Rte_{}_AckFlags.Rte_ModeSwitchAck_{}_{}_{}_Ack'.format(connected_osapplication, mode_switch_rport["connected_component_name"], mode_switch_rport["connect_port_name"], mode_switch_rport["mode_group_prototype"])}})
    {
        curMode = RTE_TRANSITION_{{mode_switch_rport["swc_name"]}}_{{mode_switch_rport["mode_group"]}};
        (void)Rte_IccPeek_Rte_M_{{mode_switch_rport["connected_component_name"]}}_{{mode_switch_rport["connect_port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}}(nextMode);
        *(previousMode) = Rte_ModeMachine_{{mode_switch_rport["connected_component_name"]}}_{{mode_switch_rport["connect_port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}};
    }
    else
    {
        curMode = Rte_ModeMachine_{{mode_switch_rport["connected_component_name"]}}_{{mode_switch_rport["connect_port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}};
        *(previousMode) = curMode;
        *(nextMode) = curMode;
    }
                {%- else %}
    curMode = Rte_ModeMachine_{{mode_switch_rport["connected_component_name"]}}_{{mode_switch_rport["connect_port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}};
    *(previousMode) = curMode;
    *(nextMode) = curMode;
                {%- endif %}
    Rte_EnableOSInterrupts();
    return curMode;
}
            {%- else %}
FUNC(uint8,RTE_CODE) Rte_Mode_{{mode_switch_rport["swc_name"]}}_{{mode_switch_rport["port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}}(void)
{
    uint8 curMode;
                {%- if mode_switch_rport["swc_mode_switch_event"] %}
    if (Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{mode_switch_rport["connected_component_name"]}}_{{mode_switch_rport["connect_port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}}_Ack {{"== 0U" if not inter_partition else '!= Rte_{}_AckFlags.Rte_ModeSwitchAck_{}_{}_{}_Ack'.format(connected_osapplication, mode_switch_rport["connected_component_name"], mode_switch_rport["connect_port_name"], mode_switch_rport["mode_group_prototype"])}})
    {
        curMode = RTE_TRANSITION_{{mode_switch_rport["swc_name"]}}_{{mode_switch_rport["mode_group"]}};
    }
    else
    {
        curMode = Rte_ModeMachine_{{mode_switch_rport["connected_component_name"]}}_{{mode_switch_rport["connect_port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}};
    }
                {%- else %}
    curMode = Rte_ModeMachine_{{mode_switch_rport["connected_component_name"]}}_{{mode_switch_rport["connect_port_name"]}}_{{mode_switch_rport["mode_group_prototype"]}};
                {%- endif %}
    return curMode;
}
            {%- endif %}
        {%- endif %}
    {%- endfor %}
{%- endif %}
{%- set mode_switch_rports_unconnected, mode_switch_pports_unconnected = get_mode_switch_port_unconnected_in_swc_list() %}
{%- if mode_switch_pports|length or mode_switch_pports_unconnected|length %}
{#- Rte_Switch API prototypes #}
/*
 * Mode Switch API (Rte_Switch)
 */
{%- for mode_switch_pport in  mode_switch_pports_unconnected %}
{%- set swc_name = mode_switch_pport["swc_name"] %}
{%- set port_name = mode_switch_pport["port_name"] %}
{%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Switch_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 nextMode)
{
    (void) nextMode;
    Std_ReturnType operationResult = RTE_E_OK;
    return operationResult;
}
{%- endfor %}
{%- for mode_switch_pport in mode_switch_pports %}
{%- set swc_name = mode_switch_pport["swc_name"] %}
{%- set port_name = mode_switch_pport["port_name"] %}
{%- set mode_group_name = mode_switch_pport["mode_group"] %}
{%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
{%- set connected_osapplication = mode_switch_pport["connected_osapplication"] %}
{%- set inter_partition = mode_switch_pport["inter_partition"] %}
{%- set if_use_internal_index_current_mode = mode_switch_pport["if_use_internal_index_current_mode"] %}
{%- set if_use_current_mode = mode_switch_pport["if_use_current_mode"] %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Switch_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(uint8 nextMode)
{
    Std_ReturnType operationResult = RTE_E_OK;
    {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
    uint8 internalIndexNextMode = Rte_GetInternalModeIndex_{{swc_name}}_{{mode_group_name}}(nextMode);
        {%- if if_use_internal_index_current_mode %}
    uint8 internalIndexCurrentMode;
        {%- endif %}
    {%- endif %}
    {%- if if_use_current_mode %}
    uint8 currentMode;
    {%- endif %}

    Rte_DisableOSInterrupts();

    {%- if if_use_current_mode %}
    currentMode = Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}};
    {%- endif %}
    {%- if if_use_internal_index_current_mode %}
    internalIndexCurrentMode = Rte_GetInternalModeIndex_{{swc_name}}_{{mode_group_name}}(currentMode);
    {%- endif %}

    do {
    {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
        if (internalIndexNextMode >= {{mode_switch_pport["mode_group_len"]}}U)
    {%- else %}
        if (nextMode >= {{mode_switch_pport["mode_group_len"]}}U)
    {%- endif %}
        {
            Rte_EnableOSInterrupts();
            operationResult = RTE_E_LIMIT;
            break;
        }
        
    {%- if mode_switch_pport["swc_mode_switch_event"] %}
        {%- set task_name = get_mode_switch_event_triggered_task(mode_switch_pport) %}
        {%- set transition_events = mode_switch_pport["swc_mode_switch_event"].get("transition_events", []) %}
        {%- set entry_events = mode_switch_pport["swc_mode_switch_event"].get("entry_events", []) %}
        {%- set exit_events = mode_switch_pport["swc_mode_switch_event"].get("exit_events", []) %}
        if (Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Ack {{"== 0U" if not inter_partition else '!= Rte_{}_AckFlags.Rte_ModeSwitchAck_{}_{}_{}_Ack'.format(connected_osapplication, swc_name, port_name, mode_group_prototype_name)}})
        {
            operationResult = Rte_IccSend_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(nextMode);
            Rte_EnableOSInterrupts();
            break;
        }

        EventMaskType ModeSwitchEventMask = (EventMaskType)0;
        {%- if exit_events|length %}
            {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
        ModeSwitchEventMask = Rte_GetModeExitEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(internalIndexCurrentMode);
            {%- else %}
        ModeSwitchEventMask = Rte_GetModeExitEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(currentMode);
            {%- endif %}
        {%- endif %}
        {%- if entry_events|length %}
            {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
        ModeSwitchEventMask |= (EventMaskType)Rte_GetModeEntryEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(internalIndexNextMode);
            {%- else %}
        ModeSwitchEventMask |= (EventMaskType)Rte_GetModeEntryEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(nextMode);
            {%- endif %}
        {%- endif %}
        {%- if transition_events|length %}
            {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
        ModeSwitchEventMask |= (EventMaskType)Rte_GetModeTransitionEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(Rte_ModeTransitionEventId_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}[internalIndexCurrentMode][internalIndexNextMode]);
            {%- else %}
        ModeSwitchEventMask |= (EventMaskType)Rte_GetModeTransitionEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(Rte_ModeTransitionEventId_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}[currentMode][nextMode]);
            {%- endif %}
        {%- endif %}
        {%- if inter_partition %}
        ModeSwitchEventMask |= (EventMaskType)RTE_EV_MS_{{task_name.upper()}};
        {%- endif %}
        if (ModeSwitchEventMask != (EventMaskType)0)
        {
            operationResult = Rte_IccSend_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(nextMode);
            Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Ack {{"= 0U" if not inter_partition else '= !Rte_{}_AckFlags.Rte_ModeSwitchAck_{}_{}_{}_Ack'.format(connected_osapplication, swc_name, port_name, mode_group_prototype_name)}};
        {%- if mode_switch_pport["mode_disabled_entity"] %}
            if (currentMode != nextMode)
            {
                Rte_ActivateModeDisabling_{{mode_switch_pport["osapplication"]}}_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(nextMode);
            }
        {%- endif %}
            Rte_EnableOSInterrupts();
#if (RTE_DLOG_ENABLED == STD_ON)
            Std_ReturnType ret_dlog = RTE_E_OK;
            ret_dlog = SetEvent((TaskType) OS_TASK_{{task_name.upper()}}_ID, ModeSwitchEventMask);
            operationResult |= ret_dlog;
            if (ret_dlog != RTE_E_OK)
            {
                Rte_LogOs(RTE_SWITCH_SERVICE_ID, Rte_Switch_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_ID, OSServiceId_SetEvent, OS_TASK_{{task_name.upper()}}_ID, ret_dlog);
            }
#else
            operationResult = SetEvent((TaskType) OS_TASK_{{task_name.upper()}}_ID, ModeSwitchEventMask);
#endif
            break;
        }
        {%- if not inter_partition %}
        Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}} = nextMode;
        Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Ack = 1U;
        Rte_EnableOSInterrupts();
        {%- endif %}
    {%- else %}
        Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}} = nextMode;
        Rte_EnableOSInterrupts();
    {%- endif %}
    } while(0);
#if (RTE_DLOG_ENABLED == STD_ON)
    if (operationResult != RTE_E_OK)
    {
        Rte_LogRte(RTE_SWITCH_SERVICE_ID, Rte_Switch_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_ID, operationResult);
    }
#endif
    
    return operationResult;
}
{% endfor -%}
{% endif -%}
{%- set mode_switch_pports_ack = get_mode_switch_ack_port_in_asw_cfg() -%}
{%- if mode_switch_pports_ack|length -%}
{#- Rte_Switch_Ack API prototypes #}
/*
 * Switch Acknowledgement handling (Rte_SwitchAck)
 */
{%- for mode_switch_pport in mode_switch_pports_ack %}
{%- set swc_name = mode_switch_pport["swc_name"] %}
{%- set port_name = mode_switch_pport["port_name"] %}
{%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
{%- set connected_osapplication = mode_switch_pport["connected_osapplication"] %}
{%- set inter_partition = mode_switch_pport["inter_partition"] %}
FUNC(Std_ReturnType,RTE_CODE) Rte_SwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(void)
{
    Std_ReturnType operationResult = RTE_E_TRANSMIT_ACK;
    {%- if mode_switch_pport["swc_mode_switch_event"] %}
    if (Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Ack {{"== 0U" if not inter_partition else '!= Rte_{}_AckFlags.Rte_ModeSwitchAck_{}_{}_{}_Ack'.format(connected_osapplication, swc_name, port_name, mode_group_prototype_name)}})
    {
        operationResult = RTE_E_NO_DATA;
    }
    {%- endif %}
#if (RTE_DLOG_ENABLED == STD_ON)
    if (operationResult != RTE_E_TRANSMIT_ACK)
    {
        Rte_LogRte(RTE_SWITCHACK_SERVICE_ID, Rte_SwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_ID, operationResult);
    }
#endif
    return operationResult;
}
{% endfor -%}
{% endif -%}

{%- set pports = get_all_mode_disabled_pports(appl_name) %}
{%- if pports %}
/*
 * RTE mode disabled entities
 */
{%- for pport in pports %}
static FUNC(void, RTE_CODE) Rte_ActivateModeDisabling_{{pport["osapplication"]}}_{{pport["swc_name"]}}_{{pport["port_name"]}}_{{pport["mode_group_prototype"]}}(uint8 nextMode)
{
    switch (nextMode)
    {
    {%- for mode, diabled_entities in pport["mode_disabled_entity"].items() %}
        case {{mode}}:
        {%- for swc_run, entity_info in diabled_entities.items() %}
            Rte_Flag_ModeDisableTrigger_{{entity_info["osa"]}}.Rte_Flag_ModeDisableTrigger_{{swc_run}} = TRUE;
        {%- endfor %}
            break;
    {%- endfor %}
        default:
            // do nothing
            break;
    }
}

static FUNC(void, RTE_CODE) Rte_DeActivateModeDisabling_{{pport["osapplication"]}}_{{pport["swc_name"]}}_{{pport["port_name"]}}_{{pport["mode_group_prototype"]}}(uint8 prevMode)
{
    switch (prevMode)
    {
    {%- for mode, diabled_entities in pport["mode_disabled_entity"].items() %}
        case {{mode}}:
        {%- for swc_run, entity_info in diabled_entities.items() %}
            Rte_Flag_ModeDisableTrigger_{{entity_info["osa"]}}.Rte_Flag_ModeDisableTrigger_{{swc_run}} = FALSE;
        {%- endfor %}
            break;
    {%- endfor %}
        default:
            // do nothing
            break;
    }
}
{# blank line #}
{%- endfor %}
{%- endif %}

{#- ---------------------------------------------------------------- #}
{#- SR implicit write / read template #}
{#- ---------------------------------------------------------------- #}

{%- macro task_sr_impl_read(task_name, runnable_name, rport_data, indent) %}
{%- set output_buff = "Rte_" + task_name + ".Rte_RB.Rte_" + rport_data["component"] + "_" + runnable_name + ".Rte_" + rport_data["port_prototype"] + "_" + rport_data["data_element"] %}
{#- ===== START intra ecu sr ===== #}
{%- if rport_data["m_to_n"] != "0" %}
{%- if rport_data["global_counter"] %}
{#- N:1 #}

{%- if rport_data["lock"] == "OSISR" %}
{{indent}}Rte_DisableOSInterrupts();
{%- elif rport_data["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{indent}}ret = GetSpinlock{{rport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport_data["lock_id"].upper()}}_ID);
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{rport_data["lock_id"].upper()}}_ID, ret);
{{indent}}}
#else
{{indent}}(void)GetSpinlock{{rport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport_data["lock_id"].upper()}}_ID);
#endif
{%- endif %}

{%- set global_counter = rport_data["global_counter"] %}
{%- set receiver_buffer = rport_data["variable_name"] %}
{{indent}}if ({{global_counter}} == 0U) {
{{indent}}    /* do nothing */
{{indent}}    ;
{%- for (sender_counter, _) in rport_data["all_sender_counter"] %}
    {%- set sender_buffer = rport_data["all_sender_buffer"][loop.index - 1][0] %}
{{indent}}} else if ({{global_counter}} == {{sender_counter}}) {
    {%- if rport_data["lock"] == "NONE" %}
        {%- if rport_data["data_category"] == "NUMERICAL" %}
            {%- if rport_data.get("buff_struct_name", None) and rport_data["sender_buff_struct_name"] and rport_data["sender_buff_struct_name"][loop.index - 1] %}
{{indent}}    {{rport_data["buff_struct_name"]}}.{{receiver_buffer}} = {{rport_data["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
            {%- elif rport_data.get("buff_struct_name", None) %}
{{indent}}    {{rport_data["buff_struct_name"]}}.{{receiver_buffer}} = {{sender_buffer}};
            {%- elif rport_data["sender_buff_struct_name"] and rport_data["sender_buff_struct_name"][loop.index - 1] %}
{{indent}}    {{receiver_buffer}} = {{rport_data["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
            {%- else %}
{{indent}}    {{receiver_buffer}} = {{sender_buffer}};
            {%- endif %}
        {%- else %}
{{indent}}    Rte_MemCpy(&{{receiver_buffer}}, &{{sender_buffer}}, sizeof({{rport_data["data_type"]["name"]}}));
        {%- endif %}
    {%- elif rport_data["lock"] == "OSISR" %}
        {%- if rport_data["data_category"] == "NUMERICAL" %}
            {%- if rport_data.get("buff_struct_name", None) and rport_data["sender_buff_struct_name"] and rport_data["sender_buff_struct_name"][loop.index - 1] %}
{{indent}}    {{rport_data["buff_struct_name"]}}.{{receiver_buffer}} = {{rport_data["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
            {%- elif rport_data.get("buff_struct_name", None) %}
{{indent}}    {{rport_data["buff_struct_name"]}}.{{receiver_buffer}} = {{sender_buffer}};
            {%- elif rport_data["sender_buff_struct_name"] and rport_data["sender_buff_struct_name"][loop.index - 1] %}
{{indent}}    {{receiver_buffer}} = {{rport_data["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
            {%- else %}
{{indent}}    {{receiver_buffer}} = {{sender_buffer}};
            {%- endif %}
        {%- else %}
{{indent}}    Rte_MemCpy(&{{receiver_buffer}}, &{{sender_buffer}}, sizeof({{rport_data["data_type"]["name"]}}));
        {%- endif %}
    {%- elif rport_data["lock"] == "SPINLOCK" %}
        {%- if rport_data["data_category"] == "NUMERICAL" %}
            {%- if rport_data.get("buff_struct_name", None) and rport_data["sender_buff_struct_name"] and rport_data["sender_buff_struct_name"][loop.index - 1] %}
{{indent}}    {{rport_data["buff_struct_name"]}}.{{receiver_buffer}} = {{rport_data["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
            {%- elif rport_data.get("buff_struct_name", None) %}
{{indent}}    {{rport_data["buff_struct_name"]}}.{{receiver_buffer}} = {{sender_buffer}};
            {%- elif rport_data["sender_buff_struct_name"] and rport_data["sender_buff_struct_name"][loop.index - 1] %}
{{indent}}    {{receiver_buffer}} = {{rport_data["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
            {%- else %}
{{indent}}    {{receiver_buffer}} = {{sender_buffer}};
            {%- endif %}
        {%- else %}
{{indent}}    Rte_MemCpy(&{{receiver_buffer}}, &{{sender_buffer}}, sizeof({{rport_data["data_type"]["name"]}}));
        {%- endif %}
    {%- endif %}
{%- endfor %}
{{indent}}} else {
{{indent}}    /* do nothing */
{{indent}}    ;
{{indent}}}
{{indent}}{{rport_data["global_counter"]}} = 0U;

{%- if rport_data["lock"] == "OSISR" %}
{{indent}}Rte_EnableOSInterrupts();
{%- elif rport_data["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{indent}}ret = ReleaseSpinlock{{rport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport_data["lock_id"].upper()}}_ID);
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{rport_data["lock_id"].upper()}}_ID, ret);
{{indent}}}
#else
{{indent}}(void)ReleaseSpinlock{{rport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport_data["lock_id"].upper()}}_ID);
#endif
{%- endif %}

{%- if rport_data["data_category"] == "NUMERICAL" %}
    {%- if rport_data.get("buff_struct_name", None) %}
{{indent}}{{output_buff}} = {{rport_data["buff_struct_name"]}}.{{receiver_buffer}};
    {%- else %}
{{indent}}{{output_buff}} = {{receiver_buffer}};
    {%- endif %}
{%- else %}
{{indent}}Rte_MemCpy(&{{output_buff}}, &{{receiver_buffer}}, sizeof({{rport_data["data_type"]["name"]}}));
{%- endif %}

{%- else %}
{#- 1:1 & 1:N #}

{%- if rport_data["lock"] == "NONE" %}
    {%- if rport_data["data_category"] == "NUMERICAL" %}
        {%- if rport_data.get("buff_struct_name", None) %}
{{indent}}{{output_buff}} = {{rport_data["buff_struct_name"]}}.{{rport_data["variable_name"]}};
        {%- else %}
{{indent}}{{output_buff}} = {{rport_data["variable_name"]}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{output_buff}}, &{{rport_data["variable_name"]}}, sizeof({{rport_data["data_type"]["name"]}}));
    {%- endif %}
{%- elif rport_data["lock"] == "OSISR" %}
{{indent}}Rte_DisableOSInterrupts();
    {%- if rport_data["data_category"] == "NUMERICAL" %}
        {%- if rport_data.get("buff_struct_name", None) %}
{{indent}}{{output_buff}} = {{rport_data["buff_struct_name"]}}.{{rport_data["variable_name"]}};
        {%- else %}
{{indent}}{{output_buff}} = {{rport_data["variable_name"]}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{output_buff}}, &{{rport_data["variable_name"]}}, sizeof({{rport_data["data_type"]["name"]}}));
    {%- endif %}
{{indent}}Rte_EnableOSInterrupts();
{%- elif rport_data["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{indent}}ret = GetSpinlock{{rport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport_data["variable_name"].upper()}}_ID);
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{rport_data["variable_name"].upper()}}_ID, ret);
{{indent}}}
#else
{{indent}}(void)GetSpinlock{{rport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport_data["variable_name"].upper()}}_ID);
#endif
    {%- if rport_data["data_category"] == "NUMERICAL" %}
        {%- if rport_data.get("buff_struct_name", None) %}
{{indent}}{{output_buff}} = {{rport_data["buff_struct_name"]}}.{{rport_data["variable_name"]}};
        {%- else %}
{{indent}}{{output_buff}} = {{rport_data["variable_name"]}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{output_buff}}, &{{rport_data["variable_name"]}}, sizeof({{rport_data["data_type"]["name"]}}));
    {%- endif %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{indent}}ret = ReleaseSpinlock{{rport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport_data["variable_name"].upper()}}_ID);
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{rport_data["variable_name"].upper()}}_ID, ret);
{{indent}}}
#else
{{indent}}(void)ReleaseSpinlock{{rport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport_data["variable_name"].upper()}}_ID);
#endif
{%- endif %}
{%- endif %}
{%- endif %}
{#- ===== END intra ecu sr ===== #}
{#- ===== START inter ecu com ===== #}
{%- if rport_data["signal_category"] in ["Signal", "SignalGroup"] %}
{{-rte_read_rx_com(rport_data, output_buff, False, "RTE_TASK_SERVICE_ID", "OS_TASK_" + task_name + "_ID", indent)}}
{%- endif %}
{#- ===== END inter ecu com ===== #}
{%- endmacro %}

{%- macro task_sr_impl_write(task_name, runnable_name, pport_data, indent) %}
{%- set and_op = "" if pport_data["arg"]["datatype"]["native_category"] == "NUMERICAL" else "&" %}
{%- set input_buff = and_op + "Rte_" + task_name + ".Rte_RB.Rte_" + pport_data["component"] + "_" + runnable_name + ".Rte_" + pport_data["port_prototype"] + "_" + pport_data["data_element"] %}
{#- ===== START intra ecu sr ===== #}
{%- if pport_data["m_to_n"] != "0" %}
{%- if pport_data["global_counter"] %}
{#- N:1 #}

{%- if pport_data["lock"] == "NONE" %}
    {%- if pport_data["data_category"] == "NUMERICAL" %}
        {%- if pport_data['sender_buff_struct_name'] and pport_data['sender_buff_struct_name'][0] %}
{{indent}}{{pport_data['sender_buff_struct_name'][0]}}.{{pport_data["variable_name"]}} = {{input_buff}};
        {%- else %}
{{indent}}{{pport_data["variable_name"]}} = {{input_buff}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{pport_data["variable_name"]}}, {{input_buff}}, sizeof({{pport_data["data_type"]["name"]}}));
    {%- endif %}
{{indent}}{{pport_data["global_counter"]}}++;
{{indent}}{{pport_data["local_counter"]}} = {{pport_data["global_counter"]}};
{%- elif pport_data["lock"] == "OSISR" %}
{{indent}}Rte_DisableOSInterrupts();
    {%- if pport_data["data_category"] == "NUMERICAL" %}
        {%- if pport_data['sender_buff_struct_name'] and pport_data['sender_buff_struct_name'][0] %}
{{indent}}{{pport_data['sender_buff_struct_name'][0]}}.{{pport_data["variable_name"]}} = {{input_buff}};
        {%- else %}
{{indent}}{{pport_data["variable_name"]}} = {{input_buff}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{pport_data["variable_name"]}}, {{input_buff}}, sizeof({{pport_data["data_type"]["name"]}}));
    {%- endif %}
{{indent}}{{pport_data["global_counter"]}}++;
{{indent}}{{pport_data["local_counter"]}} = {{pport_data["global_counter"]}};
{{indent}}Rte_EnableOSInterrupts();
{%- elif pport_data["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{indent}}ret = GetSpinlock{{pport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport_data["lock_id"].upper()}}_ID);
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{pport_data["lock_id"].upper()}}_ID, ret);
{{indent}}}
#else
    (void) TerminateTask();
{{indent}}(void)GetSpinlock{{pport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport_data["lock_id"].upper()}}_ID);
#endif
    {%- if pport_data["data_category"] == "NUMERICAL" %}
        {%- if pport_data['sender_buff_struct_name'] and pport_data['sender_buff_struct_name'][0] %}
{{indent}}{{pport_data['sender_buff_struct_name'][0]}}.{{pport_data["variable_name"]}} = {{input_buff}};
        {%- else %}
{{indent}}{{pport_data["variable_name"]}} = {{input_buff}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{pport_data["variable_name"]}}, {{input_buff}}, sizeof({{pport_data["data_type"]["name"]}}));
    {%- endif %}
{{indent}}{{pport_data["global_counter"]}}++;
{{indent}}{{pport_data["local_counter"]}} = {{pport_data["global_counter"]}};
#if (RTE_DLOG_ENABLED == STD_ON)
{{indent}}ret = ReleaseSpinlock{{pport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport_data["lock_id"].upper()}}_ID);
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{pport_data["lock_id"].upper()}}_ID, ret);
{{indent}}}
#else
{{indent}}(void)ReleaseSpinlock{{pport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport_data["lock_id"].upper()}}_ID);
#endif
{%- endif %}
{%- else %}
{#- 1:1 & 1:N #}

{%- if pport_data["lock"] == "NONE" %}
    {%- if pport_data["data_category"] == "NUMERICAL" %}
        {%- if pport_data.get("buff_struct_name", None) %}
{{indent}}{{pport_data['buff_struct_name']}}.{{pport_data["variable_name"]}} = {{input_buff}};
        {%- else %}
{{indent}}{{pport_data["variable_name"]}} = {{input_buff}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{pport_data["variable_name"]}}, {{input_buff}}, sizeof({{pport_data["data_type"]["name"]}}));
    {%- endif %}
{%- elif pport_data["lock"] == "OSISR" %}
{{indent}}Rte_DisableOSInterrupts();
    {%- if pport_data["data_category"] == "NUMERICAL" %}
        {%- if pport_data.get("buff_struct_name", None) %}
{{indent}}{{pport_data['buff_struct_name']}}.{{pport_data["variable_name"]}} = {{input_buff}};
        {%- else %}
{{indent}}{{pport_data["variable_name"]}} = {{input_buff}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{pport_data["variable_name"]}}, {{input_buff}}, sizeof({{pport_data["data_type"]["name"]}}));
    {%- endif %}
{{indent}}Rte_EnableOSInterrupts();
{%- elif pport_data["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{indent}}ret = GetSpinlock{{pport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport_data["variable_name"].upper()}}_ID);
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{pport_data["variable_name"].upper()}}_ID, ret);
{{indent}}}
#else
{{indent}}(void)GetSpinlock{{pport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport_data["variable_name"].upper()}}_ID);
#endif
    {%- if pport_data["data_category"] == "NUMERICAL" %}
        {%- if pport_data.get("buff_struct_name", None) %}
{{indent}}{{pport_data['buff_struct_name']}}.{{pport_data["variable_name"]}} = {{input_buff}};
        {%- else %}
{{indent}}{{pport_data["variable_name"]}} = {{input_buff}};
        {%- endif %}
    {%- else %}
{{indent}}Rte_MemCpy(&{{pport_data["variable_name"]}}, {{input_buff}}, sizeof({{pport_data["data_type"]["name"]}}));
    {%- endif %}
#if (RTE_DLOG_ENABLED == STD_ON)
{{indent}}ret = ReleaseSpinlock{{pport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport_data["variable_name"].upper()}}_ID);
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{pport_data["variable_name"].upper()}}_ID, ret);
{{indent}}}
#else
{{indent}}(void)ReleaseSpinlock{{pport_data["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport_data["variable_name"].upper()}}_ID);
#endif
{%- endif %}
{%- endif %}
{%- endif %}
{#- ===== END intra ecu sr ===== #}
{#- ===== START inter ecu com ===== #}
{%- if pport_data["signal_category"] in ["Signal", "SignalGroup"] %}
{{-rte_write_tx_com(pport_data, input_buff, "RTE_TASK_SERVICE_ID", "OS_TASK_" + task_name.upper() + "_ID", indent)}}
{%- endif %}
{#- ===== END inter ecu com ===== #}
{#- ===== START set events ===== #}
{%- if pport_data["set_events"] %}
#if (RTE_DLOG_ENABLED == STD_ON)
    {%- for set_event in pport_data["set_events"] %}
{{indent}}ret = SetEvent((TaskType){{set_event["task_id"].upper()}}, (EventMaskType){{set_event["event_mask"].upper()}});
{{indent}}if (ret != E_OK)
{{indent}}{
{{indent}}    Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name.upper()}}_ID, OSServiceId_SetEvent, {{set_event["task_id"].upper()}}, ret);
{{indent}}}
    {%- endfor %}
#else
    {%- for set_event in pport_data["set_events"] %}
{{indent}}(void)SetEvent((TaskType) {{set_event["task_id"].upper()}}, (EventMaskType){{set_event["event_mask"].upper()}});
    {%- endfor %}
#endif
{%- endif %}
{#- ===== END set events ===== #}
{%- endmacro %}

{#- ---------------------------------------------------------------- #}
{#- IRV implicit write / read template #}
{#- ---------------------------------------------------------------- #}

{%- set implicit_irv_write_dict, implicit_irv_read_dict = get_osa_implicit_irv_access(appl_name) %}

{%- macro task_irv_impl_write(task_name, runnable_symbol, ind) %}
{%- set accesses = implicit_irv_write_dict.get(task_name, {}).get(runnable_symbol, []) %}
{%- for write in accesses %}
    {%- if write["lock"] == "OSISR" %}
{{ind}}Rte_DisableOSInterrupts();
    {%- endif %}

    {%- if write["data"]["datatype"]["native_category"] == "NUMERICAL" %}
{{ind}}{{write["data"]["buff"]}} = {{write["buff_imp_copy"]}};
    {%- else %}
{{ind}}Rte_MemCpy(&{{write["data"]["buff"]}}, &{{write["buff_imp_copy"]}}, sizeof({{write["data"]["datatype"]["name"]}}));
    {%- endif %}

    {%- if write["lock"] == "OSISR" %}
{{ind}}Rte_EnableOSInterrupts();
    {%- endif %}
{%- endfor %}
{%- endmacro %}

{%- macro task_irv_impl_read(task_name, runnable_symbol, ind) %}
{%- set accesses = implicit_irv_read_dict.get(task_name, {}).get(runnable_symbol, []) %}
{%- for read in accesses %}
    {%- if read["lock"] == "OSISR" %}
{{ind}}Rte_DisableOSInterrupts();
    {%- endif %}

    {%- if read["data"]["datatype"]["native_category"] == "NUMERICAL" %}
{{ind}}{{read["buff_imp_copy"]}} = {{read["data"]["buff"]}};
    {%- else %}
{{ind}}Rte_MemCpy(&{{read["buff_imp_copy"]}}, &{{read["data"]["buff"]}}, sizeof({{read["data"]["datatype"]["name"]}}));
    {%- endif %}

    {%- if read["lock"] == "OSISR" %}
{{ind}}Rte_EnableOSInterrupts();
    {%- endif %}
{%- endfor %}
{%- endmacro %}


{%- set task_list = get_cur_appl_basic_task_with_st_list() %}
{%- set runnables_dict = get_all_runnables_dict() %}
{%- for task_info in task_list %}
{%- set task_name = task_info["task_name"] %}
FUNC(void,RTE_CODE) os_task_{{task_name.lower()}}(void *arg)
{
    (void)arg;
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType ret;
#endif

    {%- set schedule_table_step_info = get_schedule_table_step_info(task_name) %}
    {%- set schedule_table_step_list = schedule_table_step_info['step_list'] %}
    {%- set schedule_table_step_cycle_start = schedule_table_step_info['step_cycle_start'] %}
    {%- for schedule_table_step in schedule_table_step_list %}
    {%- if loop.first %}
    if(Rte_ScheduleTable_{{task_name}}_Step == {{loop.index0}}U)
    {
        if (Rte_InitState == RTE_STATE_INIT) {
        {%- for period in schedule_table_step %}
        {%- set runnable_list = get_timing_event_runnable_list(task_name, period) %}
        {%- for runnable_symbol in runnable_list %}
            {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
            {%- set vfb_hook_start_str = hook_tuple_list[0] %}
            {%- set vfb_hook_return_str = hook_tuple_list[1] %}
            {%- if vfb_hook_start_str %}
            {{ vfb_hook_start_str }}();
            {%- endif %}
            {{ runnable_symbol }}();
            {%- if vfb_hook_return_str %}
            {{ vfb_hook_return_str }}();
            {%- endif %}
        {%- endfor %}
        {%- endfor %}
        }
        Rte_ScheduleTable_{{task_name}}_Step = {{loop.index0 + 1}};
    }
    {%- else %}
    else if(Rte_ScheduleTable_{{task_name}}_Step == {{loop.index0}}U)
    {
        if (Rte_InitState == RTE_STATE_INIT) {
        {%- for period in schedule_table_step %}
        {%- set runnable_list = get_timing_event_runnable_list(task_name, period) %}
        {%- for runnable_symbol in runnable_list %}
            {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
            {%- set vfb_hook_start_str = hook_tuple_list[0] %}
            {%- set vfb_hook_return_str = hook_tuple_list[1] %}
            {%- if vfb_hook_start_str %}
            {{ vfb_hook_start_str }}();
            {%- endif %}
            {{ runnable_symbol }}();
            {%- if vfb_hook_return_str %}
            {{ vfb_hook_return_str }}();
            {%- endif %}
        {%- endfor %}
        {%- endfor %}
        }
        {%- if loop.last %}
        Rte_ScheduleTable_{{task_name}}_Step = {{schedule_table_step_cycle_start}};
        {%- else %}
        Rte_ScheduleTable_{{task_name}}_Step = {{loop.index0 + 1}};
        {%- endif %}
    }
    {%- endif %}
    {%- endfor %}{# {%- for schedule_table_step in schedule_table_step_list%} #}

#if (RTE_DLOG_ENABLED == STD_ON)
    ret = TerminateTask();
    if (ret != E_OK)
    {
        Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_TerminateTask, OS_TASK_{{task_name}}_ID, ret);
    }
#else
    (void) TerminateTask();
#endif
}
{% endfor -%}{# for task_info in task_list #}


{%- set task_list = get_cur_appl_basic_task_without_st_list() %}
{%- set runnables_dict = get_all_runnables_dict() %}
{%- set implicit_sr_data_dict = get_all_implicit_sr_data_dict() %}
{%- for task_info in task_list %}
{%- set task_name = task_info["task_name"] %}
FUNC(void,RTE_CODE) os_task_{{task_name.lower()}}(void *arg)
{
    (void)arg;
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType ret;
#endif

    {%- set runnable_list = get_init_event_runnable_list(task_name) %}
    {%- for runnable_symbol in runnable_list %}
    {%- set rport_data_list = implicit_sr_data_dict[runnable_symbol]["rport_data"] %}
    {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
    {%- set vfb_hook_start_str = hook_tuple_list[0] %}
    {%- set vfb_hook_return_str = hook_tuple_list[1] %}
    {%- if vfb_hook_start_str %}
    {{ vfb_hook_start_str }}();
    {%- endif %}
    {%- for rport_data in rport_data_list %}
    {%- if loop.first %}
    /* read implicit data */
    {%- endif %}
    {{task_sr_impl_read(task_name, rport_data['runnable_name'], rport_data, "    ")}}

    {%- endfor %}
    {{-task_irv_impl_read(task_name, runnable_symbol, "    ")}}

    {%- set exclusive_areas = get_runs_inside_exclusive_areas(runnable_symbol, task_name) %}
    {%- for exclusive_area in exclusive_areas %}
    {%- if exclusive_area["name"] %}
    {%- if loop.first %}
    {%- endif %}
    {{ exclusive_area["suspend"]}}({{ exclusive_area["input_param"] }});
    {%- endif %}
    {%- endfor %}
    {{ runnable_symbol }}();
    {%- for exclusive_area in exclusive_areas | reverse %}
    {%- if exclusive_area["name"] %}
    {%- if loop.first %}
    {%- endif %}
    {{ exclusive_area["resume"] }}({{ exclusive_area["input_param"] }});
    {%- endif %}
    {%- endfor %}

    {%- set pport_data_list = implicit_sr_data_dict[runnable_symbol]["pport_data"]%}
    {%- for pport_data in pport_data_list %}
    {%- if loop.first %}
    {%- endif %}
    {{-task_sr_impl_write(task_name, pport_data['runnable_name'], pport_data, "    ")}}

    {%- endfor %}
    {{-task_irv_impl_write(task_name, runnable_symbol, "    ")}}
    {%- if vfb_hook_return_str %}
    {{ vfb_hook_return_str }}();
    {%- endif %}
    {%- endfor %}

    {%- set runnable_groups = get_runnable_groups(task_name) %}
    {%- for runnable_groups in runnable_groups %}
    {%- for runnable_symbol in runnable_groups["entity"] -%}
        {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
        {%- set vfb_hook_start_str = hook_tuple_list[0] %}
        {%- set vfb_hook_return_str = hook_tuple_list[1] %}
        {%- if vfb_hook_start_str %}
    {{ vfb_hook_start_str }}();
        {%- endif %}
    {%- set rport_data_list = implicit_sr_data_dict[runnable_symbol]["rport_data"] %}
    {%- for rport_data in rport_data_list %}
    {%- if loop.first %}
    /* read implicit data */
    {%- endif %}
    {{task_sr_impl_read(task_name, rport_data['runnable_name'], rport_data, "    ")}}

    {%- endfor %}
    {{-task_irv_impl_read(task_name, runnable_symbol, "    ")}}

    {%- set exclusive_areas = get_runs_inside_exclusive_areas(runnable_symbol, task_name) %}
    {%- for exclusive_area in exclusive_areas %}
    {%- if exclusive_area["name"] %}
    {%- if loop.first %}
    {%- endif %}
    {{ exclusive_area["suspend"] }}({{ exclusive_area["input_param"] }});
    {%- endif %}
    {%- endfor %}
    {{ runnable_symbol }}();
    {%- for exclusive_area in exclusive_areas | reverse %}
    {%- if exclusive_area["name"] %}
    {%- if loop.first %}
    {%- endif %}
    {{ exclusive_area["resume"] }}({{ exclusive_area["input_param"] }});
    {%- endif %}
    {%- endfor %}

    {%- set pport_data_list = implicit_sr_data_dict[runnable_symbol]["pport_data"]%}
    {%- for pport_data in pport_data_list %}
    {%- if loop.first %}
    {%- endif %}
    {{task_sr_impl_write(task_name, pport_data['runnable_name'], pport_data, "    ")}}

    {%- endfor %}
    {{-task_irv_impl_write(task_name, runnable_symbol, "    ")}}
    {%- if vfb_hook_return_str %}
    {{ vfb_hook_return_str }}();
    {%- endif %}
    {%- endfor %}
    {%- endfor %}

#if (RTE_DLOG_ENABLED == STD_ON)
    ret = TerminateTask();
    if (ret != E_OK)
    {
        Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_TerminateTask, OS_TASK_{{task_name}}_ID, ret);
    }
#else
    (void) TerminateTask();
#endif
}
{% endfor -%}{# for task_info in task_list #}

{%- set task_list = get_cur_appl_extended_task_list() %}
{%- set runnables_dict = get_all_runnables_dict() %}
{%- set implicit_sr_data_dict = get_all_implicit_sr_data_dict() %}

{%- for task_info in task_list %}
{%- set task_name = task_info["task_name"] %}
FUNC(void,RTE_CODE) os_task_{{task_name.lower()}}(void *arg)
{
    (void)arg;
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType ret;
#endif

    {%- set event_mask_list = get_all_wait_event_mask_list(task_name) %}
    {%- if event_mask_list|length != 0 %}
    EventMaskType ev;
    {%- endif %}
    {%- set activation_vector_type = get_task_activation_vector_type(task_name) %}
    {%- if activation_vector_type | length > 0 %}
    {{activation_vector_type}} activationVector;
    {%- endif %}

    {%- set runnable_list = get_init_event_runnable_list(task_name) %}
    {%- for runnable_symbol in runnable_list %}
        {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
        {%- set vfb_hook_start_str = hook_tuple_list[0] %}
        {%- set vfb_hook_return_str = hook_tuple_list[1] %}
        {%- if vfb_hook_start_str %}
    {{ vfb_hook_start_str }}();
        {%- endif %}
    {%- set rport_data_list = implicit_sr_data_dict[runnable_symbol]["rport_data"] %}
    {%- for rport_data in rport_data_list %}
    {%- if loop.first %}
    /* read implicit data */
    {%- endif %}
    {{task_sr_impl_read(task_name, rport_data['runnable_name'], rport_data, "    ")}}

    {%- endfor %}
    {{-task_irv_impl_read(task_name, runnable_symbol, "    ")}}

    {%- set exclusive_areas = get_runs_inside_exclusive_areas(runnable_symbol, task_name) %}
    {%- for exclusive_area in exclusive_areas %}
    {%- if exclusive_area["name"] %}
    {%- if loop.first %}
    {%- endif %}
    {{ exclusive_area["suspend"]}}({{ exclusive_area["input_param"] }});
    {%- endif %}
    {%- endfor %}
    {{ runnable_symbol }}();
    {%- for exclusive_area in exclusive_areas | reverse %}
    {%- if exclusive_area["name"] %}
    {%- if loop.first %}
    {%- endif %}
    {{ exclusive_area["resume"] }}({{ exclusive_area["input_param"] }});
    {%- endif %}
    {%- endfor %}

    {%- set pport_data_list = implicit_sr_data_dict[runnable_symbol]["pport_data"]%}
    {%- for pport_data in pport_data_list %}
    {%- if loop.first %}
    {%- endif %}
    {{task_sr_impl_write(task_name, pport_data['runnable_name'], pport_data, "    ")}}

    {%- endfor %}
    {{-task_irv_impl_write(task_name, runnable_symbol, "    ")}}
    {%- if vfb_hook_return_str %}
    {{ vfb_hook_return_str }}();
    {%- endif %}
    {%- endfor %}

    {%- if event_mask_list|length != 0 %}

    for (;;) {
#if (RTE_DLOG_ENABLED == STD_ON)
        ret = WaitEvent({{get_event_masks_with_type_conversion(event_mask_list)}});
        if (ret != E_OK)
        {
            Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_WaitEvent, OS_TASK_{{task_name}}_ID, ret);
        }
        ret = GetEvent((TaskType) OS_TASK_{{task_name}}_ID, &ev);
        if (ret != E_OK)
        {
            Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_GetEvent, OS_TASK_{{task_name}}_ID, ret);
        }
        ret = ClearEvent(ev & ({{get_event_masks_with_type_conversion(event_mask_list)}}));
        if (ret != E_OK)
        {
            Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_ClearEvent, OS_TASK_{{task_name}}_ID, ret);
        }
#else
        (void)WaitEvent({{get_event_masks_with_type_conversion(event_mask_list)}});
        (void)GetEvent((TaskType) OS_TASK_{{task_name.upper()}}_ID, &ev);
        (void)ClearEvent(ev & ({{get_event_masks_with_type_conversion(event_mask_list)}}));
#endif
        {%- set ns = namespace(cp_func_done=false) %}
        {%- set max_period_ev_name = get_max_period_event_in_period_task(task_name) %}
        {%- for runnable_groups in get_runnable_groups(task_name) %}

        {%- if runnable_groups["mask"] == "BackgroundEvent" %}

        if (Rte_StartTiming_InitState == RTE_STATE_INIT) {
        {%- elif runnable_groups["has_mode_disabling"] %}

        if (!{{runnable_groups["disabling_flag"]}} && ((ev & {{runnable_groups["mask"]}}) != (EventMaskType)0)) {
        {%- else %}
        {%- if loop.length > 1 %}
        if ((ev & {{runnable_groups["mask"]}}) != (EventMaskType)0) {
        {%- endif %}
        {%- endif %}
            {%- if max_period_ev_name and max_period_ev_name in runnable_groups["mask"] and not ns.cp_func_done %}
                {%- set check_point_func_param = get_alive_check_point_func_param(task_name, max_period_ev_name) %}
                {%- set ns.cp_func_done = true %}
                {%- if "WdgM" in get_enable_module_list() %}
            WdgM_CheckpointReached({{ check_point_func_param | join(", ") }});
                {%- endif %}
            {%- endif %}

        {%- for runnable_symbol in runnable_groups["entity"] %}
            {%- set activation_reasons = get_runnable_activation_reasons(runnable_symbol, task_name)%}
            {%- for activation_reason in activation_reasons %}
                {%- if loop.first %}
            activationVector = 0U;
                {%- endif %}
            if ((ev & (EventMaskType){{activation_reason["mask"]}}) != (EventMaskType)0) {
                activationVector |= Rte_ActivatingEvent_{{runnable_symbol}}_{{activation_reason["name"]}};
            }
            {%- endfor %}
        {%- set cross_core_cs_data_dict = get_cross_core_cs_data_dict() %}
        {%- if runnable_symbol in cross_core_cs_data_dict %}
            {%- set pport = cross_core_cs_data_dict[runnable_symbol] %}
            {%- set queue = pport['queue'] %}
            {%- set port_def_args = pport['port_def_args'] %}
            uint8 clientId = 0;
#if (RTE_DLOG_ENABLED == STD_ON)
            (void) Rte_IocReceive_{{pport['component']}}_{{queue['name']}}(&clientId);
            {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
            {%- set vfb_hook_start_str = hook_tuple_list[0] %}
            {%- set vfb_hook_return_str = hook_tuple_list[1] %}
            {%- if vfb_hook_start_str %}
            {{ vfb_hook_start_str }}();
            {%- endif %}
            const Rte_CS_DataType_{{pport["component"]}}_{{pport["port_name"]}} *clientData = &Rte_CS_Data_{{pport["component"]}}_{{pport["port_name"]}}[clientId];
            {%- if pport["return_type"] == "Std_ReturnType"%}
                {%- if port_def_args %}
            *(clientData->Rte_Result) = {{runnable_symbol}}({{(get_port_def_arg_str_list(port_def_args.values()) + pport["args"]) | join(", ")}});
                {%- else %}
            *(clientData->Rte_Result) = {{runnable_symbol}}({{pport["args"] | join(", ")}});
                {%- endif %}
            {%- else %}
            {{runnable_symbol}}({{pport["args"] | join(", ")}});
            *(clientData->Rte_Result) = (Std_ReturnType)RTE_E_OK;
            {%- endif %}
            *(clientData->Rte_CallCompleted_Server) = *(clientData->Rte_CallCompleted_Client);
            {%- if vfb_hook_return_str %}
            {{ vfb_hook_return_str }}();
            {%- endif %}
            {% if pport["return_trigger_flag"] %}
            if (clientData->TriggerTask != INVALID_TASK)
            {
                ret = SetEvent((TaskType) clientData->TriggerTask, (EventMaskType)clientData->TriggerEvent);
            }

            if (ret != E_OK)
            {
                Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name.upper()}}_ID, OSServiceId_SetEvent, clientData->TriggerTask, ret);
            }
            {%- elif pport["has_waiting_event"] %}
            if (clientData->waitingTask != INVALID_TASK)
            {
                ret = SetEvent((TaskType) clientData->waitingTask, (EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK);
            }

            if (ret != E_OK)
            {
                Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name.upper()}}_ID, OSServiceId_SetEvent, clientData->waitingTask, ret);
            }
            {%- endif %}
#else
            (void) Rte_IocReceive_{{pport['component']}}_{{queue['name']}}(&clientId);
            {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
            {%- set vfb_hook_start_str = hook_tuple_list[0] %}
            {%- set vfb_hook_return_str = hook_tuple_list[1] %}
            {%- if vfb_hook_start_str %}
            {{ vfb_hook_start_str }}();
            {%- endif %}
            const Rte_CS_DataType_{{pport["component"]}}_{{pport["port_name"]}} *clientData = &Rte_CS_Data_{{pport["component"]}}_{{pport["port_name"]}}[clientId];
            {%- if pport["return_type"] == "Std_ReturnType"%}
                {%- if port_def_args %}
            *(clientData->Rte_Result) = {{runnable_symbol}}({{(get_port_def_arg_str_list(port_def_args.values()) + pport["args"]) | join(", ")}});
                {%- else %}
            *(clientData->Rte_Result) = {{runnable_symbol}}({{pport["args"] | join(", ")}});
                {%- endif %}
            {%- else %}
            {{runnable_symbol}}({{pport["args"] | join(", ")}});
            *(clientData->Rte_Result) = (Std_ReturnType)RTE_E_OK;
            {%- endif %}
            *(clientData->Rte_CallCompleted_Server) = *(clientData->Rte_CallCompleted_Client);
            {%- if vfb_hook_return_str %}
            {{ vfb_hook_return_str }}();
            {%- endif %}
            {%- if pport["return_trigger_flag"] %}
            if (clientData->TriggerTask != INVALID_TASK)
            {
                (void)SetEvent((TaskType) clientData->TriggerTask, (EventMaskType)clientData->TriggerEvent);
            }
            {%- elif pport["has_waiting_event"] %}
            if (clientData->waitingTask != INVALID_TASK)
            {
                (void)SetEvent((TaskType) clientData->waitingTask, (EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK);
            }
            {%- endif %}
#endif
        {%- else %}
            {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
            {%- set vfb_hook_start_str = hook_tuple_list[0] %}
            {%- set vfb_hook_return_str = hook_tuple_list[1] %}
            {%- if vfb_hook_start_str %}
            {{ vfb_hook_start_str }}();
            {%- endif %}
            {%- set rport_data_list = implicit_sr_data_dict[runnable_symbol]["rport_data"] %}
            {%- for rport_data in rport_data_list %}
            {%- if loop.first %}
            /* read implicit data */
            {%- endif %}
            {{task_sr_impl_read(task_name, rport_data['runnable_name'], rport_data, "            ")}}

            {%- endfor %}
            {{-task_irv_impl_read(task_name, runnable_symbol, "            ")}}

            {%- set exclusive_areas = get_runs_inside_exclusive_areas(runnable_symbol, task_name) %}
            {%- for exclusive_area in exclusive_areas %}
            {%- if exclusive_area["name"] %}
            {%- if loop.first %}
            {%- endif %}
            {{ exclusive_area["suspend"] }}({{ exclusive_area["input_param"] }});
            {%- endif %}
            {%- endfor %}
            {%- set runnable_args = get_runnable_args(runnable_symbol, task_name) %}
            {{ runnable_symbol }}({{runnable_args}});
            {%- for exclusive_area in exclusive_areas | reverse %}
            {%- if exclusive_area["name"] %}
            {%- if loop.first %}
            {%- endif %}
            {{ exclusive_area["resume"] }}({{ exclusive_area["input_param"] }});
            {%- endif %}
            {%- endfor %}

            {%- set pport_data_list = implicit_sr_data_dict[runnable_symbol]["pport_data"]%}
            {%- for pport_data in pport_data_list %}
            {%- if loop.first %}
            {%- endif %}
            {{task_sr_impl_write(task_name, pport_data['runnable_name'], pport_data, "            ")}}

            {%- endfor %}
            {{-task_irv_impl_write(task_name, runnable_symbol, "            ")}}
            {%- if vfb_hook_return_str %}
            {{ vfb_hook_return_str }}();
            {%- endif %}
        {%- endif %}{#- {%- if runnable_symbol in cross_core_cs_data_dict %} #}
        {%- endfor %}{#- for runnable_symbol in runnable_groups["entity"]#}
        {%- if loop.length > 1 %}
        }
        {%- endif %}
        {%- endfor %}{#- for runnable_groups in get_runnable_groups(task) #}
        {%- set appl_task_mode_switch_port_list = get_appl_task_mode_switch_port_list(appl_name, task_name)%}
        {%- for mode_switch_pport in appl_task_mode_switch_port_list %}
        {%- set swc_name = mode_switch_pport["swc_name"] %}
        {%- set port_name = mode_switch_pport["port_name"] %}
        {%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
        {%- set mode_group_name = mode_switch_pport["mode_group"] %}
        {%- set connected_osapplication = mode_switch_pport["connected_osapplication"] %}
        {%- set inter_partition = mode_switch_pport["inter_partition"] %}
        {%- set osapplication = mode_switch_pport["osapplication"] %}
        {%- set mode_switch_event_mask_list = get_all_mode_switch_event_mask_list(appl_name, task_name, mode_switch_pport)%}
        {%- if mode_switch_event_mask_list|length != 0 %}

        if (((ev & ({{get_event_masks_with_type_conversion(mode_switch_event_mask_list)}})) != (EventMaskType)0) && (Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Ack {{"== 0U" if not inter_partition else '!= Rte_{}_AckFlags.Rte_ModeSwitchAck_{}_{}_{}_Ack'.format(osapplication, swc_name, port_name, mode_group_prototype_name)}}))
        {
        {%- if mode_switch_pport["queue_length"] > 1 %}
            uint8 l_ret;
            uint8 nextMode = 0U;
            boolean doAnotherSwitch = (boolean)FALSE;
            uint8 curMode = Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}};
            Rte_DisableOSInterrupts();
            (void)Rte_IccReceive_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(&nextMode);
            {%- if mode_switch_pport["mode_disabled_entity"] %}
            if (curMode != nextMode)
            {
                Rte_DeActivateModeDisabling_{{osapplication}}_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(curMode);
            }
            {%- endif %}
            Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}} = nextMode;
            l_ret = Rte_IccPeek_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(&nextMode);
            if (l_ret != RTE_E_NO_DATA)
            {
                doAnotherSwitch = (boolean)TRUE;
                curMode = Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}};
            }

            if (l_ret == RTE_E_NO_DATA)
            {
                Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Ack {{"= 1U" if not inter_partition else '= Rte_{}_AckFlags.Rte_ModeSwitchAck_{}_{}_{}_Ack'.format(osapplication, swc_name, port_name, mode_group_prototype_name)}};
            }
            Rte_EnableOSInterrupts();
            if (doAnotherSwitch == (boolean)TRUE)
            {
                EventMaskType ModeSwitchEventMask = (EventMaskType)0;
                {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
                uint8 internalIndexNextMode;
                uint8 internalIndexCurrentMode;
                internalIndexNextMode = Rte_GetInternalModeIndex_{{swc_name}}_{{mode_group_name}}(nextMode);
                internalIndexCurrentMode = Rte_GetInternalModeIndex_{{swc_name}}_{{mode_group_name}}(curMode);
                if ((internalIndexNextMode <{{mode_switch_pport["mode_group_len"]}}U) && (internalIndexCurrentMode < {{mode_switch_pport["mode_group_len"]}}U))
                {%- else %}
                if ((nextMode < {{mode_switch_pport["mode_group_len"]}}U) && (curMode < {{mode_switch_pport["mode_group_len"]}}U))
                {%- endif %}
                {
                {%- set transition_events = mode_switch_pport["swc_mode_switch_event"].get("transition_events", []) %}
                {%- set entry_events = mode_switch_pport["swc_mode_switch_event"].get("entry_events", []) %}
                {%- set exit_events = mode_switch_pport["swc_mode_switch_event"].get("exit_events", []) %}
                {%- if exit_events|length %}
                    {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
                    ModeSwitchEventMask = Rte_GetModeExitEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(internalIndexCurrentMode);
                    {%- else %}
                    ModeSwitchEventMask = Rte_GetModeExitEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(curMode);
                    {%- endif %}
                {%- endif %}
                {%- if entry_events|length %}
                    {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
                    ModeSwitchEventMask |= (EventMaskType)Rte_GetModeEntryEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(internalIndexNextMode);
                    {%- else %}
                    ModeSwitchEventMask |= (EventMaskType)Rte_GetModeEntryEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(nextMode);
                    {%- endif %}
                {%- endif %}
                {%- if transition_events|length %}
                    {%- if mode_switch_pport["mode_group_category"] == "EXPLICIT_ORDER" %}
                    ModeSwitchEventMask |= (EventMaskType)Rte_GetModeTransitionEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(Rte_ModeTransitionEventId_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}[internalIndexCurrentMode][internalIndexNextMode]);
                    {%- else %}
                    ModeSwitchEventMask |= (EventMaskType)Rte_GetModeTransitionEventMask_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(Rte_ModeTransitionEventId_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}[curMode][nextMode]);
                    {%- endif %}
                {%- endif %}
                    ModeSwitchEventMask |= (EventMaskType)RTE_EV_MS_{{task_name.upper()}};
                }
                else
                {
                    ModeSwitchEventMask = (EventMaskType)0;
                }

                if ((ModeSwitchEventMask) != (EventMaskType)0)
                {
                {%- if mode_switch_pport["mode_disabled_entity"] %}
                    Rte_DisableOSInterrupts();
                    if (curMode != nextMode)
                    {
                        Rte_ActivateModeDisabling_{{osapplication}}_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(nextMode);
                    }
                    Rte_EnableOSInterrupts();
                {%- endif %}
#if (RTE_DLOG_ENABLED == STD_ON)
                    l_ret = SetEvent((TaskType) OS_TASK_{{task_name.upper()}}_ID, (EventMaskType)ModeSwitchEventMask);
                    if (l_ret != E_OK)
                    {
                        Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name.upper()}}_ID, OSServiceId_SetEvent, OS_TASK_{{task_name.upper()}}_ID, l_ret);
                    }
#else
                    l_ret = SetEvent((TaskType) OS_TASK_{{task_name.upper()}}_ID, (EventMaskType)ModeSwitchEventMask);
#endif

                }
            }
        {%- else %}{# {%- mode_switch_pport["queue_length"] > 1 %} #}
            uint8 nextMode = 0U;
            Rte_DisableOSInterrupts();
            (void)Rte_IccReceive_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(&nextMode);
            {%- if mode_switch_pport["mode_disabled_entity"] %}
            uint8 prevMode = Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}};
            if (prevMode != nextMode)
            {
                Rte_DeActivateModeDisabling_{{osapplication}}_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}(prevMode);
            }
            {%- endif %}
            Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}} = nextMode;
            Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Ack {{"= 1U" if not inter_partition else '= !Rte_{}_AckFlags.Rte_ModeSwitchAck_{}_{}_{}_Ack'.format(osapplication, swc_name, port_name, mode_group_prototype_name)}};
            Rte_EnableOSInterrupts();
        {%- endif %}{# {%- mode_switch_pport["queue_length"] > 1 %} #}
        }
        {%- endif %}{# {%- if mode_switch_event_mask_list|length != 0 %} #}
        {%- endfor %}{# for mode_switch_port in appl_task_mode_switch_port_list #}
    }
    {%- else %}
    {%- set runnable_groups = get_runnable_groups(task_name) %}
    {%- if only_background_event(task_name) %}
    for(;;) {
        {%- for runnable_group in runnable_groups %}

        {%- for runnable_symbol in runnable_group["entity"] -%}
        if (Rte_StartTiming_InitState == RTE_STATE_INIT) {
            {%- set hook_tuple_list = get_vfb_runnable_hook_name(runnable_symbol) %}
            {%- set vfb_hook_start_str = hook_tuple_list[0] %}
            {%- set vfb_hook_return_str = hook_tuple_list[1] %}
            {%- if vfb_hook_start_str %}
            {{ vfb_hook_start_str }}();
            {%- endif %}
            {{ runnable_symbol }}();
            {%- if vfb_hook_return_str %}
            {{ vfb_hook_return_str }}();
            {%- endif %}
        }
        {%- endfor %}
        {%- endfor %}
    }
    {%- endif %}{# {%- if only_background_event(task_name) %} #}

    {%- if runnable_list|length != 0 %}
#if (RTE_DLOG_ENABLED == STD_ON)
    ret = TerminateTask();
    if (ret != E_OK)
    {
        Rte_LogOs(RTE_TASK_SERVICE_ID, OS_TASK_{{task_name}}_ID, OSServiceId_TerminateTask, OS_TASK_{{task_name}}_ID, ret);
    }
#else
    (void) TerminateTask();
#endif
    {%- endif %}

    {%- endif %}{# if event_mask_list|length != 0 #}
}
{% endfor -%}{# for task_info in task_list #}

/*
 * Extended Life-Cycle API implementation
 */
FUNC(void,RTE_CODE) Rte_InitMemory_{{appl_name}}(void)
{

{%- set buffers = get_sr_nonqueue_variable_dict_by_appl(appl_name) %}
{%- set nonzero_boolean_vars = get_sr_nonqueue_intra_osa_boolean_variable_dict_by_appl(appl_name, 'nonzero') -%}
{%- for buffer in buffers.values() %}
    {%- set datatype = buffer['arg']['datatype'] %}
    {%- set init_value = buffer["arg"].get("init_value", {}).get("value", {}) %}
    {%- if buffer.get('init_zero', false) %}
        {#- init value is zero #}
    {%- elif datatype['native_category'] == 'NUMERICAL' %}
        {%- set basetype = datatype["basetype"] %}
        {%- if basetype[0] == "b" %}        {#- boolean #}
            {%- if nonzero_boolean_vars and buffer["variable"] in nonzero_boolean_vars[appl_name] %}
    {{buffer["buff_struct_name"]}}.{{buffer["variable"]}} = TRUE;
            {%- else %}
                {%- if init_value.get("value", "FALSE") == "FALSE" or init_value.get("value", "FALSE") == "False" or init_value.get("value", "FALSE") == "0" %}
    {{buffer["variable"]}} = FALSE;
                {%- elif init_value.get("value", "FALSE") == "TRUE" or init_value.get("value", "FALSE") == "True" or init_value.get("value", "FALSE") == "1" %}
    {{buffer["variable"]}} = TRUE;
                {%- else %}
    {{buffer["variable"]}} = FALSE;
                {%- endif %}
            {%- endif %}
        {%- elif basetype[0] == "u" %}      {#- unsigned #}
    {{buffer["variable"]}} = {{init_value.get("value", "0U")}};
        {%- elif basetype[0] == "s" %}      {#- signed #}
    {{buffer["variable"]}} = {{init_value.get("value", "0")}};
        {%- elif basetype[0] == "f" %}      {#- float #}
    {{buffer["variable"]}} = {{init_value.get("value", "0.0F")}};
        {%- endif %}
    {%- elif datatype['native_category'] == 'STRUCTURE' %}
        {%- if 'Explicit' in buffer['type'] %}
    {{buffer["variable"]}} = {{init_value['name']}};
        {%- else %}
    {#- TODO: initmemory for implicit data #}
        {%- endif %}
    {%- else %}
        {%- if 'Explicit' in buffer['type'] %}
    Rte_MemCpy(&{{buffer["variable"]}}, &{{init_value['name']}}, sizeof({{datatype["name"]}}));
        {%- else %}
    {#- TODO: initmemory for implicit data #}
        {%- endif %}
    {%- endif %}
{%- endfor %}

{%- set mode_switch_pports = get_appl_task_mode_switch_port_list(appl_name, with_pport = "yes") %}
{%- if mode_switch_pports|length %}
    Rte_{{appl_name}}_AckFlagsInit();
    /* reset Tx Ack Flags */
{%- endif %}
{%- for mode_switch_pport in mode_switch_pports %}
{%- set swc_name = mode_switch_pport["swc_name"] %}
{%- set port_name = mode_switch_pport["port_name"] %}
{%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
    Rte_{{appl_name}}_AckFlags.Rte_ModeSwitchAck_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Ack = 1U;
{%- endfor %}
{%- if mode_switch_pports|length %}
    /* mode management initialization part 1 */
{%- endif %}
{%- for mode_switch_pport in mode_switch_pports %}
{%- set swc_name = mode_switch_pport["swc_name"] %}
{%- set port_name = mode_switch_pport["port_name"] %}
{%- set mode_group_prototype_name = mode_switch_pport["mode_group_prototype"] %}
{%- set mode_group_name = mode_switch_pport["mode_group"] %}
{%- set init_mode_name = mode_switch_pport["init_mode"] %}
{%- set inter_partition = mode_switch_pport["inter_partition"] %}
{%- set queue_length = mode_switch_pport["queue_length"] %}
{%- set osapplication = mode_switch_pport["osapplication"] %}
{%- if osapplication == appl_name %}
    {%- for id in range(queue_length) %}
    Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_Queue[{{id}}U] = RTE_MODE_{{swc_name}}_{{mode_group_name}}_{{init_mode_name}};
    {%- endfor %}
{%- endif %}
{%- set connected_osapplication = mode_switch_pport["connected_osapplication"] %}
    {%- if inter_partition and connected_osapplication and connected_osapplication == appl_name %}
    Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}} = RTE_MODE_{{swc_name}}_{{mode_group_name}}_{{init_mode_name}};
    Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head = 0;
    {%- elif inter_partition and osapplication and osapplication == appl_name %}
    Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail = 0;
    {%- else %}
    Rte_ModeMachine_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}} = RTE_MODE_{{swc_name}}_{{mode_group_name}}_{{init_mode_name}};
    Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_tail = 0;
    Rte_ioc_Rte_M_{{swc_name}}_{{port_name}}_{{mode_group_prototype_name}}_head = 0;
    {%- endif %}
{%- endfor %}

{%- set pports = get_all_mode_disabled_pports(appl_name) %}
{%- if pports %}
    /* reset mode disabling flags */
    Rte_MemClr(&Rte_Flag_ModeDisableTrigger_{{appl_name}}, sizeof(Rte_Flag_ModeDisableTrigger_{{appl_name}}_Type));
    {%- for pport in pports %}
        {%- for swc_run, entity_info in pport["mode_disabled_flag"].items() %}
            {%- if pport["init_mode"] in entity_info["disabled_mode"] %}
    Rte_Flag_ModeDisableTrigger_{{appl_name}}.Rte_Flag_ModeDisableTrigger_{{swc_run}} = TRUE;
            {%- endif %}
        {%- endfor %}
    {%- endfor %}
{%- endif %}

{%- set vars, vars_no_init_value = get_osa_irv_var(appl_name) %}
{%- for irv in vars %}
{%- set init_value, is_zero, is_bool = get_sr_var_init_value(irv["datatype"], irv["init_value"]) %}
    {{irv["buff"]}} = {{init_value}};
{%- endfor %}

{%- set vars, vars_no_init_value = get_osa_pim_var(appl_name) %}
{%- for pim in vars %}
{%- set init_value, is_zero, is_bool = get_sr_var_init_value(pim["datatype"], pim["init_value"]) %}
    {{pim["buff"]}} = {{init_value}};
{%- endfor %}
}

{#- ---------------------------------------------------------------- -#}
{#- rte_write function body -#}
{#- ---------------------------------------------------------------- -#}

{%- macro rte_write_multi_buff_primitive(pport, datatype) %}
{%- if pport["lock"] == "SPINLOCK" or pport["set_events"] | length > 0 %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
#endif
{%- endif %}
{%- if pport["lock"] == "NONE" %}
    {%- if pport.get("buff_struct_name", None) %}
    {{pport['buff_struct_name']}}.{{pport["buffer_name"]}} = data;
    {%- else %}
    {{pport["buffer_name"]}} = data;
    {%- endif %}
    {{pport["global_counter"]}}++;
    {{pport["local_counter"]}} = {{pport["global_counter"]}};
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
{%- elif pport["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
    {%- if pport.get("buff_struct_name", None) %}
    {{pport['buff_struct_name']}}.{{pport["buffer_name"]}} = data;
    {%- else %}
    {{pport["buffer_name"]}} = data;
    {%- endif %}
    {{pport["global_counter"]}}++;
    {{pport["local_counter"]}} = {{pport["global_counter"]}};
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    Rte_EnableOSInterrupts();
{%- elif pport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = GetSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_WRITE_SERVICE_ID, Rte_Write_{{pport["component"]}}_{{pport["name"]}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID, retLog);
    }
    {%- if pport.get("buff_struct_name", None) %}
    {{pport['buff_struct_name']}}.{{pport["buffer_name"]}} = data;
    {%- else %}
    {{pport["buffer_name"]}} = data;
    {%- endif %}
    {{pport["global_counter"]}}++;
    {{pport["local_counter"]}} = {{pport["global_counter"]}};
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    retLog = ReleaseSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_WRITE_SERVICE_ID, Rte_Write_{{pport["component"]}}_{{pport["name"]}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID, retLog);
    }
#else
    (void)GetSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID);
    {%- if pport.get("buff_struct_name", None) %}
    {{pport['buff_struct_name']}}.{{pport["buffer_name"]}} = data;
    {%- else %}
    {{pport["buffer_name"]}} = data;
    {%- endif %}
    {{pport["global_counter"]}}++;
    {{pport["local_counter"]}} = {{pport["global_counter"]}};
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    (void)ReleaseSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID);
#endif
{%- endif %}
{%- endmacro %}

{%- macro rte_write_multi_buff_complex(pport, datatype) %}
{%- if pport["lock"] == "SPINLOCK" or pport["set_events"] | length > 0 %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
#endif
{%- endif %}
{%- if pport["lock"] == "NONE" %}
    {{rte_write_complex_data(datatype['native_category'], pport['buffer_name'], datatype['name'])}}
    {{pport["global_counter"]}}++;
    {{pport["local_counter"]}} = {{pport["global_counter"]}};
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
{%- elif pport["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
    {{rte_write_complex_data(datatype['native_category'], pport['buffer_name'], datatype['name'])}}
    {{pport["global_counter"]}}++;
    {{pport["local_counter"]}} = {{pport["global_counter"]}};
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    Rte_EnableOSInterrupts();
{%- elif pport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = GetSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_WRITE_SERVICE_ID, Rte_Write_{{pport["component"]}}_{{pport["name"]}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID, retLog);
    }
    {{rte_write_complex_data(datatype['native_category'], pport['buffer_name'], datatype['name'])}}
    {{pport["global_counter"]}}++;
    {{pport["local_counter"]}} = {{pport["global_counter"]}};
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    retLog = ReleaseSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_WRITE_SERVICE_ID, Rte_Write_{{pport["component"]}}_{{pport["name"]}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID, retLog);
    }
#else
    (void)GetSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID);
    {{rte_write_complex_data(datatype['native_category'], pport['buffer_name'], datatype['name'])}}
    {{pport["global_counter"]}}++;
    {{pport["local_counter"]}} = {{pport["global_counter"]}};
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    (void)ReleaseSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["lock_id"].upper()}}_ID);
#endif
{%- endif %}
{%- endmacro %}

{%- macro rte_write_single_rec_buff_primitive(pport, datatype) %}
{%- if pport["lock"] == "SPINLOCK" or pport["set_events"] | length > 0 %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog = E_OK;
#endif
{%- endif %}
{%- if pport["lock"] == "NONE" %}
    {%- if pport.get("buff_struct_name", None) %}
    {{pport['buff_struct_name']}}.{{pport["buffer_name"]}} = data;
    {%- else %}
    {{pport["buffer_name"]}} = data;
    {%- endif %}
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
{%- elif pport["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
    {%- if pport.get("buff_struct_name", None) %}
    {{pport['buff_struct_name']}}.{{pport["buffer_name"]}} = data;
    {%- else %}
    {{pport["buffer_name"]}} = data;
    {%- endif %}
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    Rte_EnableOSInterrupts();
{%- elif pport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = GetSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_WRITE_SERVICE_ID, Rte_Write_{{pport["component"]}}_{{pport["name"]}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID, retLog);
    }
    {%- if pport.get("buff_struct_name", None) %}
    {{pport['buff_struct_name']}}.{{pport["buffer_name"]}} = data;
    {%- else %}
    {{pport["buffer_name"]}} = data;
    {%- endif %}
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    retLog = ReleaseSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_WRITE_SERVICE_ID, Rte_Write_{{pport["component"]}}_{{pport["name"]}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID, retLog);
    }
#else
    (void)GetSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID);
    {%- if pport.get("buff_struct_name", None) %}
    {{pport['buff_struct_name']}}.{{pport["buffer_name"]}} = data;
    {%- else %}
    {{pport["buffer_name"]}} = data;
    {%- endif %}
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    (void)ReleaseSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID);
#endif
{%- endif %}
{%- endmacro %}

{%- macro rte_write_single_rec_buff_complex(pport, datatype) %}
{%- if pport["lock"] == "SPINLOCK" or pport["set_events"] | length > 0 %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog = E_OK;
#endif
{%- endif %}
{%- if pport["lock"] == "NONE" %}
    {{rte_write_complex_data(datatype['native_category'], pport['buffer_name'], datatype['name'])}}
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
{%- elif pport["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
    {{rte_write_complex_data(datatype['native_category'], pport['buffer_name'], datatype['name'])}}
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    Rte_EnableOSInterrupts();
{%- elif pport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = GetSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_WRITE_SERVICE_ID, Rte_Write_{{pport["component"]}}_{{pport["name"]}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID, retLog);
    }
    {{rte_write_complex_data(datatype['native_category'], pport['buffer_name'], datatype['name'])}}
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    retLog = ReleaseSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_WRITE_SERVICE_ID, Rte_Write_{{pport["component"]}}_{{pport["name"]}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID, retLog);
    }
#else
    (void)GetSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID);
    {{rte_write_complex_data(datatype['native_category'], pport['buffer_name'], datatype['name'])}}
    {%- for update_flag in pport['enable_update'] %}
    {{update_flag}} = TRUE;
    {%- endfor %}
    (void)ReleaseSpinlock{{pport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{pport["buffer_name"].upper()}}_ID);
#endif
{%- endif %}
{%- endmacro %}

{%- macro generate_sr_set_events(port, rte_write_service_id, rte_write_api_id) -%}
{%- set merged_events = get_sr_merged_events_with_same_task(port) %}
#if (RTE_DLOG_ENABLED == STD_ON)
        {%- for set_event in merged_events %}
    retLog = SetEvent((TaskType){{set_event["task_id"].upper()}}, (EventMaskType){{set_event["event_mask"].upper()}});
    if (retLog != E_OK)
    {
        Rte_LogOs({{rte_write_service_id}}, {{rte_write_api_id}}, OSServiceId_SetEvent, {{set_event["task_id"].upper()}}, retLog);
    }
        {%- endfor %}
#else
        {%- for set_event in merged_events %}
    (void)SetEvent((TaskType) {{set_event["task_id"].upper()}}, (EventMaskType){{set_event["event_mask"].upper()}});
        {%- endfor %}
#endif
{%- endmacro %}


/*
 * Internal/External Tx connections
 */
{#- API implementation for intra-ECU S/R Explicit Tx #}
{%- set pports = get_rte_write_func_pports_with_no_prport() %}
{%- for pport in pports %}
    {%- set datatype = pport["arg"]["datatype"] %}
    {%- set data_arg = get_sr_write_data_arg(datatype) %}
    {%- set rte_write_service_id = "RTE_WRITE_SERVICE_ID" %}
    {%- set rte_write_api_id = "Rte_Write_" + pport["component"] + "_" + pport["name"] + "_ID" %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Write_{{pport["component"]}}_{{pport["name"]}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
{#- ===== start intra ECU ===== #}
{%- if pport["m_to_n"] in ["11", "1n", "n1"] %}
    {%- if pport["m_to_n"] == "n1" %}
    {#- N:1 #}
        {%- if pport["single_rec_buff_for_n1"] %}
            {%- if datatype["native_category"] == "NUMERICAL" %}
    {{-rte_write_single_rec_buff_primitive(pport, datatype)}}
            {%- else %}
    {{-rte_write_single_rec_buff_complex(pport, datatype)}}
            {%- endif %}
        {%- else %}
            {%- if datatype["native_category"] == "NUMERICAL" %}
    {{-rte_write_multi_buff_primitive(pport, datatype)}}
            {%- else %}
    {{-rte_write_multi_buff_complex(pport, datatype)}}
            {%- endif %}
        {%- endif %}
    {%- else %}
    {#- 1:1 & 1:1 #}
        {%- if datatype["native_category"] == "NUMERICAL" %}
    {{-rte_write_single_rec_buff_primitive(pport, datatype)}}
        {%- else %}
    {{-rte_write_single_rec_buff_complex(pport, datatype)}}
        {%- endif %}
    {%- endif %}

    {%- if pport["set_events"] %}
    {{-generate_sr_set_events(pport, rte_write_service_id, rte_write_api_id)}}
    {%- endif %}
{%- endif %}
{#- ===== end intra ECU ===== #}
{#- ===== start inter ECU COM ===== #}
    {{-rte_write_tx_com(pport, "data", rte_write_service_id, rte_write_api_id, "    ")}}
{#- ===== end inter ECU COM ===== #}
    return ret;
}
{% endfor %}

{# API implementation for intra-ECU S/R Explicit Tx connected with PR-Port #}
{%- set pports = get_appl_not_signal_api_hasprport_pport_list() %}
{%- if pports %}
    {%- for pport in pports %}
        {%- set datatype = pport["arg"]["datatype"] %}
        {%- set data_arg = get_sr_write_data_arg(datatype) %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Write_{{pport["component"]}}_{{pport["name"]}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
        {%- if datatype["native_category"] == "NUMERICAL" %}
    {{-rte_write_single_rec_buff_primitive(pport, datatype)}}
        {%- else %}
    {{-rte_write_single_rec_buff_complex(pport, datatype)}}
        {%- endif %}
    return ret;
}
    {% endfor %}
{%- endif %}

{#- API Implementation for S/R Queue Tx #}
{%- set pports = get_appl_sr_queue_pport_list(appl_name) %}
{%- for pport in pports %}
    {%- set datatype = pport["arg"]["datatype"] %}
    {%- set data_arg = get_sr_write_data_arg(datatype) %}
FUNC(Std_ReturnType, RTE_CODE) Rte_Send_{{pport["component"]}}_{{pport["port_prototype"]}}_{{pport['data_element']}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
    {%- if pport["set_events"] | length > 0 %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog = E_OK;
#endif
    {%- endif %}
    {%- for queue_name, queue in pport['queue'].items() %}
        {%- if queue['ioc_type'] == 'NONE' %}
            {%- if datatype['native_category'] == 'NUMERICAL' %}
    if (Rte_Enqueue_{{appl_name}}(RTE_Q_{{queue_name}}, (const void*)(&data)) == RTE_Q_FULL)
            {%- else %}
    if (Rte_Enqueue_{{appl_name}}(RTE_Q_{{queue_name}}, (const void*)(data)) == RTE_Q_FULL)
            {%- endif %}
    {
        Rte_DisableOSInterrupts();
        Rte_QOverflow_{{appl_name}}.Rte_{{queue_name}} = 1U;
        Rte_EnableOSInterrupts();
        ret = RTE_E_LIMIT;
    }
        {%- else %}
            {%- if datatype['native_category'] == 'NUMERICAL' %}
    ret |= Rte_IocSend_{{pport["component"]}}_{{pport["port_prototype"]}}_{{queue_name}}(data);
            {%- else %}
    ret |= Rte_IocSend_{{pport["component"]}}_{{pport["port_prototype"]}}_{{queue_name}}(data);
            {%- endif %}
        {%- endif %}
    {%- endfor %}

    {%- if pport["set_events"] %}
#if (RTE_DLOG_ENABLED == STD_ON)
        {%- for set_event in pport["set_events"] %}
    retLog = SetEvent((TaskType){{set_event["task_id"]}}, (EventMaskType){{set_event["event_mask"]}});
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_SEND_SERVICE_ID, Rte_Send_{{pport["component"]}}_{{pport["port_prototype"]}}_{{pport['data_element']}}_ID, OSServiceId_SetEvent, {{set_event["task_id"]}}, retLog);
    }
        {%- endfor %}
#else
        {%- for set_event in pport["set_events"] %}
    (void)SetEvent((TaskType){{set_event["task_id"].upper()}}, (EventMaskType){{set_event["event_mask"].upper()}});
        {%- endfor %}
#endif
    {%- endif %}

#if (RTE_DLOG_ENABLED == STD_ON)
    if (ret != RTE_E_OK)
    {
        Rte_LogRte(RTE_SEND_SERVICE_ID, Rte_Send_{{pport["component"]}}_{{pport["port_prototype"]}}_{{pport['data_element']}}_ID, ret);
    }
#endif
    return ret;
}
{%- endfor %}

{#- ---------------------------------------------------------------- -#}
{#- rte_read / dread function body -#}
{#- ---------------------------------------------------------------- -#}

{%- macro rte_dread_multi_buff(rport, datatype) -%}
FUNC({{rport["arg"]["datatype"]["name"]}}, RTE_CODE) Rte_DRead_{{rport["component"]}}_{{rport["name"]}}(void)
{
    {{rport["arg"]["datatype"]["name"]}} local_buffer;
{%- if rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
#endif
{%- endif %}

{%- if rport["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
{%- elif rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = GetSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_READ_SERVICE_ID, Rte_DRead_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID, retLog);
    }
#else
    (void)GetSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
#endif
{%- endif %}

{%- set global_counter = rport["global_counter"] %}
    if ({{global_counter}} == 0U) {
        /* do nothing */
        ;
{%- for sender_counter, _ in rport["all_sender_counter"] %}
    {%- set sender_buffer = rport["all_sender_buffer"][loop.index - 1][0] %}
    } else if ({{global_counter}} == {{sender_counter}}) {
        {%- if rport.get("buff_struct_name", None) and rport["sender_buff_struct_name"] and rport["sender_buff_struct_name"][loop.index - 1] %}
        {{rport["buff_struct_name"]}}.{{rport["buffer_name"]}} = {{rport["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
        {%- elif rport.get("buff_struct_name", None) %}
        {{rport["buff_struct_name"]}}.{{rport["buffer_name"]}} = {{sender_buffer}};
        {%- elif rport["sender_buff_struct_name"] and rport["sender_buff_struct_name"][loop.index - 1] %}
        {{rport["buffer_name"]}} = {{rport["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
        {%- else %}
        {{rport["buffer_name"]}} = {{sender_buffer}};
        {%- endif %}
{%- endfor %}
    } else {
        /* do nothing */
        ;
    }
    {{rport["global_counter"]}} = 0U;
    local_buffer = {{rport["buffer_name"]}};
{%- if rport['enable_update'] %}
    Rte_RxUpdate_{{rport['component']}}_{{rport['name']}} = FALSE;
{%- endif %}
{%- if rport["lock"] == "OSISR" %}
    Rte_EnableOSInterrupts();
{%- elif rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = ReleaseSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_READ_SERVICE_ID, Rte_DRead_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID, retLog);
    }
#else
    (void)ReleaseSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
#endif
{%- endif %}

    return local_buffer;
}
{%- endmacro %}

{%- macro rte_dread_single_rec_buff(rport, datatype) -%}
FUNC({{rport["arg"]["datatype"]["name"]}}, RTE_CODE) Rte_DRead_{{rport["component"]}}_{{rport["name"]}}(void)
{
    {{rport["arg"]["datatype"]["name"]}} local_buffer;
{%- if rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
#endif
{%- endif %}

{%- if rport["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
{%- elif rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = GetSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_READ_SERVICE_ID, Rte_DRead_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID, retLog);
    }
#else
    (void)GetSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
#endif
{%- endif %}
    {%- if rport.get("buff_struct_name", None) %}
    local_buffer = {{rport["buff_struct_name"]}}.{{rport["buffer_name"]}};
    {%- else %}
    local_buffer = {{rport["buffer_name"]}};
    {%- endif %}
{%- if rport['enable_update'] %}
    Rte_RxUpdate_{{rport['component']}}_{{rport['name']}} = FALSE;
{%- endif %}
{%- if rport["lock"] == "OSISR" %}
    Rte_EnableOSInterrupts();
{%- elif rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = ReleaseSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_READ_SERVICE_ID, Rte_DRead_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID, retLog);
    }
#else
    (void)ReleaseSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
#endif
{%- endif %}
    return local_buffer;
}
{%- endmacro %}

{%- macro rte_read_multi_buff(rport, datatype) -%}
{%- set data_arg = get_sr_read_data_arg(datatype) -%}
FUNC(Std_ReturnType,RTE_CODE) Rte_Read_{{rport["component"]}}_{{rport["name"]}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
{%- if rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
#endif
{%- endif %}

{%- if rport["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
{%- elif rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = GetSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_READ_SERVICE_ID, Rte_Read_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID, retLog);
    }
#else
    (void)GetSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
#endif
{%- endif %}

{%- set global_counter = rport["global_counter"] %}
    if ({{global_counter}} == 0U) {
        /* do nothing */
        ;
{%- for sender_counter, _ in rport["all_sender_counter"] %}
    {%- set sender_buffer = rport["all_sender_buffer"][loop.index - 1][0] %}
    } else if ({{global_counter}} == {{sender_counter}}) {
    {%- if datatype["native_category"] == 'NUMERICAL' %}
        {%- if rport.get("buff_struct_name", None) and rport["sender_buff_struct_name"] and rport["sender_buff_struct_name"][loop.index - 1] %}
        {{rport["buff_struct_name"]}}.{{rport["buffer_name"]}} = {{rport["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
        {%- elif rport.get("buff_struct_name", None) %}
        {{rport["buff_struct_name"]}}.{{rport["buffer_name"]}} = {{sender_buffer}};
        {%- elif rport["sender_buff_struct_name"] and rport["sender_buff_struct_name"][loop.index - 1] %}
        {{rport["buffer_name"]}} = {{rport["sender_buff_struct_name"][loop.index - 1]}}.{{sender_buffer}};
        {%- else %}
        {{rport["buffer_name"]}} = {{sender_buffer}};
        {%- endif %}
    {%- elif datatype["native_category"] == 'STRUCTURE' %}
        {{rport["buffer_name"]}} = {{sender_buffer}};
    {%- else %}
        Rte_MemCpy(&{{rport["buffer_name"]}}, &{{sender_buffer}}, sizeof({{datatype["name"]}}));
    {%- endif %}
{%- endfor %}
    } else {
        /* do nothing */
        ;
    }
    {{rport["global_counter"]}} = 0U;
{%- if rport['enable_update'] %}
    Rte_RxUpdate_{{rport['component']}}_{{rport['name']}} = FALSE;
{%- endif %}
{%- if rport["lock"] == "OSISR" %}
    Rte_EnableOSInterrupts();
{%- elif rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = ReleaseSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_READ_SERVICE_ID, Rte_Read_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID, retLog);
    }
#else
    (void)ReleaseSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["lock_id"].upper()}}_ID);
#endif
{%- endif %}

{%- if datatype["native_category"] == 'NUMERICAL' %}
    {%- if rport.get("buff_struct_name", None) %}
    *data = {{rport["buff_struct_name"]}}.{{rport["buffer_name"]}};
    {%- else %}
    *data = {{rport["buffer_name"]}};
    {%- endif %}
{%- elif datatype["native_category"] == 'STRUCTURE' %}
    *data = {{rport["buffer_name"]}};
{%- else %}
    Rte_MemCpy(data, &{{rport["buffer_name"]}}, sizeof({{datatype["name"]}}));
{%- endif %}
    return ret;
}
{%- endmacro %}

{%- macro rte_read_single_rec_buff(rport, datatype) -%}
{%- set data_arg = get_sr_read_data_arg(datatype) -%}
FUNC(Std_ReturnType,RTE_CODE) Rte_Read_{{rport["component"]}}_{{rport["name"]}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
{%- if rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
#endif
{%- endif %}

{%- if rport["lock"] == "NONE" %}
{%- elif rport["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
{%- elif rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = GetSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["buffer_name"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_READ_SERVICE_ID, Rte_Read_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_GetSpinlock, OS_SPINLOCK_{{rport["buffer_name"].upper()}}_ID, retLog);
    }
#else
    (void)GetSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["buffer_name"].upper()}}_ID);
#endif
{%- endif %}

{%- if datatype["native_category"] == 'NUMERICAL' %}
    {%- if rport.get("buff_struct_name", None) %}
    *data = {{rport["buff_struct_name"]}}.{{rport["buffer_name"]}};
    {%- else %}
    *data = {{rport["buffer_name"]}};
    {%- endif %}
{%- elif datatype["native_category"] == 'STRUCTURE' %}
    *data = {{rport["buffer_name"]}};
{%- else %}
    Rte_MemCpy(data, &{{rport["buffer_name"]}}, sizeof({{datatype["name"]}}));
{%- endif %}
{%- if rport['enable_update'] %}
    Rte_RxUpdate_{{rport['component']}}_{{rport['name']}} = FALSE;
{%- endif %}
{%- if rport["lock"] == "NONE" %}
{%- elif rport["lock"] == "OSISR" %}
    Rte_EnableOSInterrupts();
{%- elif rport["lock"] == "SPINLOCK" %}
#if (RTE_DLOG_ENABLED == STD_ON)
    retLog = ReleaseSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["buffer_name"].upper()}}_ID);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_READ_SERVICE_ID, Rte_Read_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_ReleaseSpinlock, OS_SPINLOCK_{{rport["buffer_name"].upper()}}_ID, retLog);
    }
#else
    (void)ReleaseSpinlock{{rport["spinlock_api_postfix"]}}((SpinlockIdType)OS_SPINLOCK_{{rport["buffer_name"].upper()}}_ID);
#endif
{%- endif %}
    return ret;
}
{%- endmacro %}

/*
 * Internal/External Rx connections
 */
{#- API Implementation for intra-ECU S/R Explicit Rx #}
{%- set rports = get_appl_not_signal_api_rport_list() %}
{%- for rport in rports %}
    {%- set datatype = rport["arg"]["datatype"] %}
    {%- if rport["read_by_value"] %}
        {%- if rport["m_to_n"] == "n1" %}
            {#- "Rte_DRead N:1" #}
            {%- if rport["single_rec_buff_for_n1"] %}
{{rte_dread_single_rec_buff(rport, datatype)}}
            {%- else %}
{{rte_dread_multi_buff(rport, datatype)}}
            {%- endif %}
        {%- else %}
            {#- "Rte_DRead 1:1 / 1:N scen with lock e.g. U64" #}
            {%- if rport["lock"] != "NONE" %}
{{rte_dread_single_rec_buff(rport, datatype)}}
            {%- endif %}
        {%- endif %}
    {%- else %}
        {%- if rport["m_to_n"] == "n1" %}
            {#- "Rte_Read N:1" #}
            {%- if rport["single_rec_buff_for_n1"] %}
{{rte_read_single_rec_buff(rport, datatype)}}
            {%- else %}
{{rte_read_multi_buff(rport, datatype)}}
            {%- endif %}
        {%- else %}
            {#- "Rte_Read 1:1 & 1:N" #}
{{rte_read_single_rec_buff(rport, datatype)}}
        {%- endif %}
    {%- endif %}
{% endfor %}

{# API implementation for intra-ECU S/R Explicit Rx connected with PR-Port #}
{%- set rports = get_appl_not_signal_api_hasprport_rport_list() %}
{%- if rports %}
    {%- for rport in rports %}
    {%- set datatype = rport["arg"]["datatype"] %}
{{rte_read_single_rec_buff(rport, datatype)}}
    {% endfor %}
{%- endif %}

{#- ---------------------------------------------------------------- #}
{#- SR queued: rte_receive #}
{#- ---------------------------------------------------------------- #}

{#- API Implementation for S/R Queue Rx #}
{%- set rports = get_appl_sr_queue_non_ioc_rport_list(appl_name) %}
{%- for rport in rports %}
    {%- set datatype = rport["arg"]["datatype"] %}
    {%- set data_arg = get_sr_read_data_arg(datatype) %}
    {#- only one queue instance(ioc or not) for on rport #}
    {%- set queue_name = rport['queue'].keys()|list|first %}
    {%- set queue = rport['queue'].values()|list|first %}
FUNC(Std_ReturnType, RTE_CODE) Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
    Std_ReturnType lost_data = RTE_E_OK;
    Rte_QReturnType retQueue = RTE_Q_OK;
    {%- if rport['timeout'] > 0 %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
#endif
    ScheduleTableType schtbl;
    EventMaskType ev;
    {%- endif %}

    Rte_DisableOSInterrupts();
    if (Rte_QOverflow_{{appl_name}}.Rte_{{queue_name}} == 1U)
    {
        Rte_QOverflow_{{appl_name}}.Rte_{{queue_name}} = 0U;
        lost_data = RTE_E_LOST_DATA;
#if (RTE_DLOG_ENABLED == STD_ON)
        Rte_LogRte(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, lost_data);
#endif
    }
    Rte_EnableOSInterrupts();

    retQueue = Rte_Dequeue_{{appl_name}}(RTE_Q_{{queue_name}}, (void*)(data));
    if (retQueue == RTE_Q_EMPTY)
    {
    {%- set task_name = rport['task'][0].upper() %}
    {%- if rport['timeout'] <= 0 %}
        ret = RTE_E_NO_DATA;
    {%- else %}
#if (RTE_DLOG_ENABLED == STD_ON)
        schtbl = (ScheduleTableType){{rport["schedule_table"]["id"]}};
        retLog = StartScheduleTableRel(schtbl, RTE_START_SCH_TBL_OFFSET);
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_StartScheduleTableRel, schtbl, retLog);
        }
        do
        {
            retLog = WaitEvent(((EventMaskType) RTE_EV_WAITPOINT_{{task_name}}) | ((EventMaskType) RTE_EV_TIMEOUT_{{task_name}}));
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_WaitEvent, RTE_DLOG_EMPTY_ID, retLog);
            }
            retLog = GetEvent((TaskType) OS_TASK_{{task_name}}_ID, &ev);
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_GetEvent, RTE_DLOG_EMPTY_ID, retLog);
            }
            retLog = ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{task_name}} | (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}));
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_ClearEvent, RTE_DLOG_EMPTY_ID, retLog);
            }

            if ((ev & (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}) == (EventMaskType)RTE_EV_TIMEOUT_{{task_name}})
            {
                ret = RTE_E_TIMEOUT;
                break;
            }
            retQueue = Rte_Dequeue_{{appl_name}}(RTE_Q_{{queue_name}}, (void*)(data));
        }
        while (retQueue == RTE_Q_EMPTY);
        if (ret != RTE_E_TIMEOUT)
        {
            retLog = StopScheduleTable(schtbl);
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_StopScheduleTable, schtbl, retLog);
            }
        }
#else
        schtbl = (ScheduleTableType){{rport["schedule_table"]["id"]}};
        (void)StartScheduleTableRel(schtbl, RTE_START_SCH_TBL_OFFSET);
        do
        {
            (void)WaitEvent(((EventMaskType) RTE_EV_WAITPOINT_{{task_name}}) | ((EventMaskType) RTE_EV_TIMEOUT_{{task_name}}));
            (void)GetEvent((TaskType) OS_TASK_{{task_name}}_ID, &ev);
            (void)ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{task_name}} | (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}));

            if ((ev & (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}) == (EventMaskType)RTE_EV_TIMEOUT_{{task_name}})
            {
                ret = RTE_E_TIMEOUT;
                break;
            }
            retQueue = Rte_Dequeue_{{appl_name}}(RTE_Q_{{queue_name}}, (void*)(data));
        }
        while (retQueue == RTE_Q_EMPTY);
        if (ret != RTE_E_TIMEOUT)
        {
            (void)StopScheduleTable(schtbl);
        }
#endif
    {%- endif %}
    }
    {%- if rport['timeout'] > 0 %}
    else
    {
        (void)GetEvent((TaskType) OS_TASK_{{task_name}}_ID, &ev);
        (void)ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{task_name}} | (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}));
    }
    {%- endif %}

#if (RTE_DLOG_ENABLED == STD_ON)
    if (ret != RTE_E_OK)
    {
        Rte_LogRte(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, ret);
    }
#endif
    ret |= lost_data;
    return ret;
}
{%- endfor%}

{%- set rports = get_appl_sr_queue_ioc_rport_list(appl_name) %}
{%- for rport in rports %}
    {%- set datatype = rport["arg"]["datatype"] %}
    {%- set data_arg = get_sr_read_data_arg(datatype) %}
    {#- only one queue instance(ioc or not) for on rport #}
    {%- set queue_name = rport['queue'].keys()|list|first %}
    {%- set queue = rport['queue'].values()|list|first %}
FUNC(Std_ReturnType, RTE_CODE) Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
    {%- if rport['timeout'] > 0 %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
#endif
    ScheduleTableType schtbl;
    EventMaskType ev;
    {%- endif %}

    ret = Rte_IocReceive_{{rport["component"]}}_{{queue_name}}(data);
    {%- set task_name = rport['task'][0].upper() %}
    {%- if rport['timeout'] > 0 %}
    if (ret == RTE_E_NO_DATA)
    {
#if (RTE_DLOG_ENABLED == STD_ON)
        schtbl = (ScheduleTableType){{rport["schedule_table"]["id"]}};
        retLog = StartScheduleTableRel(schtbl, RTE_START_SCH_TBL_OFFSET);;
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_StartScheduleTableRel, schtbl, retLog);
        }
        do
        {
            retLog = WaitEvent(((EventMaskType) RTE_EV_WAITPOINT_{{task_name}}) | ((EventMaskType) RTE_EV_TIMEOUT_{{task_name}}));
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_WaitEvent, RTE_DLOG_EMPTY_ID, retLog);
            }
            retLog = GetEvent((TaskType) OS_TASK_{{task_name}}_ID, &ev);
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_GetEvent, RTE_DLOG_EMPTY_ID, retLog);
            }
            retLog = ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{task_name}} | (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}));
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_ClearEvent, RTE_DLOG_EMPTY_ID, retLog);
            }

            if ((ev & (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}) == (EventMaskType)RTE_EV_TIMEOUT_{{task_name}})
            {
                ret = RTE_E_TIMEOUT;
                break;
            }
            ret = Rte_IocReceive_{{rport["component"]}}_{{queue_name}}(data);
        }
        while (ret == RTE_E_NO_DATA);
        if (ret != RTE_E_TIMEOUT)
        {
            retLog = StopScheduleTable(schtbl);
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, OSServiceId_StopScheduleTable, schtbl, retLog);
            }
        }
#else
        schtbl = (ScheduleTableType){{rport["schedule_table"]["id"]}};
        (void)StartScheduleTableRel(schtbl, RTE_START_SCH_TBL_OFFSET);;
        do
        {
            (void)WaitEvent(((EventMaskType) RTE_EV_WAITPOINT_{{task_name}}) | ((EventMaskType) RTE_EV_TIMEOUT_{{task_name}}));
            (void)GetEvent((TaskType) OS_TASK_{{task_name}}_ID, &ev);
            (void)ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{task_name}} | (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}));

            if ((ev & (EventMaskType)RTE_EV_TIMEOUT_{{task_name}}) == (EventMaskType)RTE_EV_TIMEOUT_{{task_name}})
            {
                ret = RTE_E_TIMEOUT;
                break;
            }
            ret = Rte_IocReceive_{{rport["component"]}}_{{queue_name}}(data);
        }
        while (ret == RTE_E_NO_DATA);
        if (ret != RTE_E_TIMEOUT)
        {
            (void)StopScheduleTable(schtbl);
        }
#endif
    }
    {%- endif %}

#if (RTE_DLOG_ENABLED == STD_ON)
    if (ret != RTE_E_OK)
    {
        Rte_LogRte(RTE_RECEIVE_SERVICE_ID, Rte_Receive_{{rport["component"]}}_{{rport["port_prototype"]}}_{{rport['data_element']}}_ID, ret);
    }
#endif
    return ret;
}
{%- endfor %}

{#- API implementation for Rx from COM #}
{%- set rports = get_appl_signal_api_rport_list() %}
{%- for rport in rports %}
    {%- set datatype = rport["arg"]["datatype"] %}
    {%- set data_arg = get_sr_read_data_arg(datatype) %}
    {%- set is_received_flag = rport["data_mapping_flag"].get("is_received", {}) %}
    {%- set is_timeout_flag = rport["data_mapping_flag"].get("is_timeout", {}) %}
    {%- set os_application = rport.get("osapplication", None) %}
    {%- set rte_read_service_id = "RTE_READ_SERVICE_ID" %}
    {%- set rte_com_api_id = "Rte_Read_" + rport["component"] + "_" + rport["name"] + "_ID" %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Read_{{rport["component"]}}_{{rport["name"]}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
    {{-rte_read_rx_com(rport, "data", True, rte_read_service_id, rte_com_api_id, "    ")}}

    {%- if is_received_flag %}
    if (!{{is_received_flag["parent_struct_name"]}}.{{is_received_flag["name"]}})
    {
        ret = RTE_E_NEVER_RECEIVED;
    }
    {%- endif %}
    {%- if is_timeout_flag %}
    if ({{is_timeout_flag["parent_struct_name"]}}.{{is_timeout_flag["name"]}})
    {
        ret |= RTE_E_MAX_AGE_EXCEEDED;
    }
    {%- endif %}
    return ret;
}
{%- endfor %}


{#- API Implementation for C/S -#}
{%- set rports_unconnected = get_appl_cs_unconnected_rports(appl_name) %}
{#}
{%- set rports = get_all_cs_multicore_rports() %}
{#}
{%- if rports_unconnected%}
/*
 * Internal C/S connections
 */
    {%- for rport in rports_unconnected%}
        {%- if "osapplication" in rport and rport["osapplication"] == appl_name %}
{%- set operation_args = get_args_str_list(rport["operation"]["args"].values()) %}
FUNC(Std_ReturnType, RTE_CODE) Rte_Call_{{rport["component"]}}_{{rport["name"]}}({{"void" if operation_args | length == 0 else operation_args | join(", ")}})
{
    Std_ReturnType ret = RTE_E_UNCONNECTED;
            {%- for arg in get_args_str_list(rport["operation"]["args"].values()) %}
    (void){{arg.split(" ")[-1]}};
            {%- endfor %}
    return ret;
}
        {%- endif %}
    {%- endfor%}
{%- endif %}

{#- API Implementation for C/S Sync #}
{%- for rport in get_appl_sync_cs_multicore_without_timeout_rports(appl_name) %}
{%- set connected_pport = rport["connected_ports"]["ports"][0] %}
{%- set arg_list = get_args_str_list(rport["operation"]["args"].values()) | join(", ") %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Call_{{rport["component"]}}_{{rport["name"]}}({{"void" if arg_list | length == 0 else arg_list}})
{
    Std_ReturnType ret = RTE_E_OK;
    EventMaskType ev;
    uint8 clientId = Rte_CS_ClientConfigIndex_{{rport["component"]}}_{{rport["name"]}};
    {%- if rport['no_dup_tasks']|length > 1 %}
    TaskType task;
    (void)GetTaskID(&task);
    {%- endif %}

    const Rte_CS_DataType_{{connected_pport["component"]}}_{{connected_pport["name"]}} *clientData = &Rte_CS_Data_{{connected_pport["component"]}}_{{connected_pport["name"]}}[clientId];
    *clientData->Rte_CallCompleted_Client = !(*clientData->Rte_CallCompleted_Server);
{%- for arg in get_appl_cs_in_args(rport["operation"]["args"]) %}
{%- if arg["direction"] == "IN" %}
{%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
    Rte_MemCpy(clientData->{{arg["name"]}}, {{arg["name"]}}, sizeof({{arg["type"]}}));
{%- else %}
    *clientData->{{arg["name"]}} = {{arg["name"]}};
{%- endif %}
{%- elif arg["direction"] == "INOUT" %}
{%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
    Rte_MemCpy(clientData->{{arg["name"]}}, {{arg["name"]}}, sizeof({{arg["type"]}}));
{%- else %}
    *clientData->{{arg["name"]}} = *{{arg["name"]}};
{%- endif %}
{%- endif %}
{%- endfor%}
    {%- if rport['no_dup_tasks']|length > 1 %}
    clientData->waitingTask = task;
    {%- endif %}

    {%- set queue = rport['queue'].values()|list|first %}
    ret = Rte_IocSend_{{rport['component']}}_{{queue['client_name']}}(clientId);
    if (ret != RTE_E_OK)
    {
#if (RTE_DLOG_ENABLED == STD_ON)
        Rte_LogRte(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, ret);
#endif
        return ret;
    }

    {%- set the_same_task = rport['no_dup_tasks'][0].upper() %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog = SetEvent((TaskType){{rport["set_events"][0]["task_id"]}}, (EventMaskType){{rport["set_events"][0]["event_mask"]}});
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_SetEvent, {{rport["set_events"][0]["task_id"]}}, retLog);
    }
    {
        do
        {
        {%- if rport['no_dup_tasks']|length > 1 %}
            retLog = WaitEvent((EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK);
        {%- else %}
            retLog = WaitEvent((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}});
        {%- endif %}
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_WaitEvent, RTE_DLOG_EMPTY_ID, retLog);
            }
        {%- if rport['no_dup_tasks']|length > 1 %}
            retLog = GetEvent(task, &ev);
        {%- else %}
            retLog = GetEvent((TaskType) OS_TASK_{{the_same_task}}_ID, &ev);
        {%- endif %}
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_GetEvent, RTE_DLOG_EMPTY_ID, retLog);
            }
        {%- if rport['no_dup_tasks']|length > 1 %}
            retLog = ClearEvent(ev & ((EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK));
        {%- else %}
            retLog = ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}}));
        {%- endif %}
            if (retLog != E_OK)
            {
                Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_ClearEvent, RTE_DLOG_EMPTY_ID, retLog);
            }
        }
        while(*clientData->Rte_CallCompleted_Client != *clientData->Rte_CallCompleted_Server);
    }
#else
    (void)SetEvent((TaskType) {{rport["set_events"][0]["task_id"].upper()}}, (EventMaskType){{rport["set_events"][0]["event_mask"].upper()}});
    {
        do
        {
        {%- if rport['no_dup_tasks']|length > 1 %}
            (void)WaitEvent((EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK);
            (void)GetEvent(task, &ev);
            (void)ClearEvent(ev & ((EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK));
        {%- else %}
            (void)WaitEvent((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}});
            (void)GetEvent((TaskType) OS_TASK_{{the_same_task}}_ID, &ev);
            (void)ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}}));
        {%- endif %}
        }
        while(*clientData->Rte_CallCompleted_Client != *clientData->Rte_CallCompleted_Server);
    }
#endif

{%- for arg in get_appl_cs_out_args(rport["operation"]["args"]) %}
    {%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
    Rte_MemCpy({{arg["name"]}}, clientData->{{arg["name"]}}, sizeof({{arg["type"]}}));
    {%- else %}
    *{{arg["name"]}} = *clientData->{{arg["name"]}};
    {%- endif %}
{%- endfor %}
    return *clientData->Rte_Result;
}
{%- endfor %}

{%- for rport in get_appl_sync_cs_multicore_timeout_rports(appl_name) %}
{%- set connected_pport = rport["connected_ports"]["ports"][0] %}
{%- set arg_list = get_args_str_list(rport["operation"]["args"].values()) | join(", ") %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Call_{{rport["component"]}}_{{rport["name"]}}({{"void" if arg_list | length == 0 else arg_list}})
{
    Std_ReturnType ret = RTE_E_OK;

    EventMaskType ev;
    ScheduleTableType schtbl;

    uint8 clientId = Rte_CS_ClientConfigIndex_{{rport["component"]}}_{{rport["name"]}};
    {%- if rport['no_dup_tasks']|length > 1 %}
    TaskType task;
    (void)GetTaskID(&task);
    {%- endif %}

    const Rte_CS_DataType_{{connected_pport["component"]}}_{{connected_pport["name"]}} *clientData = &Rte_CS_Data_{{connected_pport["component"]}}_{{connected_pport["name"]}}[clientId];
    *clientData->Rte_CallCompleted_Client = !(*clientData->Rte_CallCompleted_Server);
{%- for arg in get_appl_cs_in_args(rport["operation"]["args"]) %}
{%- if arg["direction"] == "IN" %}
{%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
    Rte_MemCpy(clientData->{{arg["name"]}}, {{arg["name"]}}, sizeof({{arg["type"]}}));
{%- else %}
    *clientData->{{arg["name"]}} = {{arg["name"]}};
{%- endif %}
{%- elif arg["direction"] == "INOUT" %}
{%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
    Rte_MemCpy(clientData->{{arg["name"]}}, {{arg["name"]}}, sizeof({{arg["type"]}}));
{%- else %}
    *clientData->{{arg["name"]}} = *{{arg["name"]}};
{%- endif %}
{%- endif %}
{%- endfor%}
    {%- if rport['no_dup_tasks']|length > 1 %}
    clientData->waitingTask = task;
    {%- endif %}

    {%- set queue = rport['queue'].values()|list|first %}
    ret = Rte_IocSend_{{rport['component']}}_{{queue['client_name']}}(clientId);
    if (ret != RTE_E_OK)
    {
#if (RTE_DLOG_ENABLED == STD_ON)
        Rte_LogRte(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, ret);
#endif
        return ret;
    }

    {%- set the_same_task = rport['no_dup_tasks'][0].upper() %}
    {%- if rport['no_dup_tasks']|length > 1 %}
        {#- TODO #}
    {%- else %}
    schtbl = (ScheduleTableType){{rport["schedule_table"]["id"]}};
    {%- endif %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog = StartScheduleTableRel(schtbl, RTE_START_SCH_TBL_OFFSET);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_StartScheduleTableRel, schtbl, retLog);
    }
    retLog = SetEvent((TaskType) {{rport["set_events"][0]["task_id"].upper()}}, (EventMaskType){{rport["set_events"][0]["event_mask"].upper()}});
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_SetEvent, {{rport["set_events"][0]["task_id"].upper()}}, retLog);
    }

    do
    {
    {%- if rport['no_dup_tasks']|length > 1 %}
        retLog = WaitEvent((EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK | (EventMaskType)RTE_TASK_TIMEOUT_EVENT_MASK);
    {%- else %}
        retLog = WaitEvent((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}} | (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}});
    {%- endif %}
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_WaitEvent, RTE_DLOG_EMPTY_ID, retLog);
        }
    {%- if rport['no_dup_tasks']|length > 1 %}
        retLog = GetEvent(task, &ev);
    {%- else %}
        retLog = GetEvent((TaskType) OS_TASK_{{the_same_task}}_ID, &ev);
    {%- endif %}
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_GetEvent, RTE_DLOG_EMPTY_ID, retLog);
        }
    {%- if rport['no_dup_tasks']|length > 1 %}
        retLog = ClearEvent(ev & ((EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK | (EventMaskType)RTE_TASK_TIMEOUT_EVENT_MASK));
    {%- else %}
        retLog = ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}} | (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}}));
    {%- endif %}
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_ClearEvent, RTE_DLOG_EMPTY_ID, retLog);
        }

    {%- if rport['no_dup_tasks']|length > 1 %}
        if ((ev & (EventMaskType))RTE_TASK_WAITPOINT_EVENT_MASK == (EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK)
    {%- else %}
        if ((ev & (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}}) == (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}})
    {%- endif %}
        {
            ret = RTE_E_TIMEOUT;
        }
    }
    while((ret != RTE_E_TIMEOUT) && (*clientData->Rte_CallCompleted_Client != *clientData->Rte_CallCompleted_Server));

    if (ret != RTE_E_TIMEOUT)
    {
        StatusType osStatus = StopScheduleTable(schtbl);
        if (osStatus != E_OK)
        {
            Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_StopScheduleTable, schtbl, osStatus);
        }
    }
#else
    (void)StartScheduleTableRel(schtbl, RTE_START_SCH_TBL_OFFSET);
    (void)SetEvent((TaskType) {{rport["set_events"][0]["task_id"].upper()}}, (EventMaskType){{rport["set_events"][0]["event_mask"].upper()}});

    do
    {
    {%- if rport['no_dup_tasks']|length > 1 %}
        (void)WaitEvent((EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK | (EventMaskType)RTE_TASK_TIMEOUT_EVENT_MASK);
        (void)GetEvent(task, &ev);
        (void)ClearEvent(ev & ((EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK | (EventMaskType)RTE_TASK_TIMEOUT_EVENT_MASK));
    {%- else %}
        (void)WaitEvent((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}} | (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}});
        (void)GetEvent((TaskType)OS_TASK_{{the_same_task}}_ID, &ev);
        (void)ClearEvent(ev & ((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}} | (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}}));
    {%- endif %}

    {%- if rport['no_dup_tasks']|length > 1 %}
        if ((ev & (EventMaskType))RTE_TASK_WAITPOINT_EVENT_MASK == (EventMaskType)RTE_TASK_WAITPOINT_EVENT_MASK)
    {%- else %}
        if ((ev & (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}}) == (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}})
    {%- endif %}
        {
            ret = RTE_E_TIMEOUT;
        }
    }
    while((ret != RTE_E_TIMEOUT) && (*clientData->Rte_CallCompleted_Client != *clientData->Rte_CallCompleted_Server));

    if (ret != RTE_E_TIMEOUT)
    {
        (void)StopScheduleTable(schtbl);
    }
#endif
    if (ret != RTE_E_TIMEOUT)
    {
    {%- for arg in get_appl_cs_out_args(rport["operation"]["args"]) %}
        {%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
        Rte_MemCpy({{arg["name"]}}, clientData->{{arg["name"]}}, sizeof({{arg["type"]}}));
        {%- else %}
        *{{arg["name"]}} = *clientData->{{arg["name"]}};
        {%- endif %}
    {%- endfor %}
        ret = *clientData->Rte_Result;
    }
#if (RTE_DLOG_ENABLED == STD_ON)
    else
    {
        Rte_LogRte(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, ret);
    }
#endif

    return ret;
}
{% endfor %}

{#- API Implementation for C/S Async #}
{%- for rport in get_appl_async_cs_multicore_rports(appl_name) %}
{%- set connected_pport = rport["connected_ports"]["ports"][0] %}
{%- set arg_list = get_input_args_str_list(rport["operation"]["args"].values())%}
{%- set sch_tbl_id = rport["schedule_table"]["id"] %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Call_{{rport["component"]}}_{{rport["name"]}}({{"void" if arg_list | length == 0 else arg_list | join(", ")}})
{
    Std_ReturnType ret = RTE_E_OK;
    uint8 clientId = Rte_CS_ClientConfigIndex_{{rport["component"]}}_{{rport["name"]}};

    const Rte_CS_DataType_{{connected_pport["component"]}}_{{connected_pport["name"]}} *clientData = &Rte_CS_Data_{{connected_pport["component"]}}_{{connected_pport["name"]}}[clientId];
    *clientData->Rte_CallCompleted_Client = !(*clientData->Rte_CallCompleted_Server);
    {%- for arg in get_appl_cs_in_args(rport["operation"]["args"]) %}
        {%- if arg["direction"] == "IN" %}
            {%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
    Rte_MemCpy(clientData->{{arg["name"]}}, {{arg["name"]}}, sizeof({{arg["type"]}}));
            {%- else %}
    *clientData->{{arg["name"]}} = {{arg["name"]}};
            {%- endif %}
        {%- elif arg["direction"] == "INOUT" %}
            {%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
    Rte_MemCpy(clientData->{{arg["name"]}}, {{arg["name"]}}, sizeof({{arg["type"]}}));
            {%- else %}
    *clientData->{{arg["name"]}} = {{arg["name"]}};
            {%- endif %}
        {%- endif %}
    {%- endfor%}
    {%- set queue = rport['queue'].values()|list|first %}
    ret = Rte_IocSend_{{rport['component']}}_{{queue['client_name']}}(clientId);
    if (ret != RTE_E_OK)
    {
#if (RTE_DLOG_ENABLED == STD_ON)
        Rte_LogRte(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, ret);
#endif
        return ret;
    }

#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog;
{%- if rport["return_mode"] == "polling" and rport["timeout"] != 0 and rport["schedule_table"] %}
    retLog = StartScheduleTableRel((ScheduleTableType){{sch_tbl_id}}, (TickType)0);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_StartScheduleTableRel, {{sch_tbl_id}}, retLog);
    }
{%- endif %}
    retLog = SetEvent((TaskType) {{rport["set_events"][0]["task_id"].upper()}}, (EventMaskType){{rport["set_events"][0]["event_mask"].upper()}});
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_CALL_SERVICE_ID, Rte_Call_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_SetEvent, {{rport["set_events"][0]["task_id"]}}, retLog);
    }
#else
{%- if rport["return_mode"] == "polling" and rport["timeout"] != 0 and rport["schedule_table"] %}
    (void)StartScheduleTableRel((ScheduleTableType){{sch_tbl_id}}, (TickType)0);
{%- endif %}
    (void)SetEvent((TaskType) {{rport["set_events"][0]["task_id"].upper()}}, (EventMaskType){{rport["set_events"][0]["event_mask"].upper()}});
#endif

    return ret;
}
{% endfor %}

{%- macro rte_result_cp_result_out(rport, ind) %}
{%- for arg in get_appl_cs_out_args(rport["operation"]["args"]) %}
    {%- if arg["category"] in ["ARRAY", "STRUCTURE", "UNION"] %}
{{ind}}Rte_MemCpy({{arg["name"]}}, clientData->{{arg["name"]}}, sizeof({{arg["type"]}}));
    {%- else %}
{{ind}}*{{arg["name"]}} = *clientData->{{arg["name"]}};
    {%- endif %}
{%- endfor %}
{{ind}}ret = *clientData->Rte_Result;
{%- endmacro %}

{%- for rport in get_appl_async_cs_multicore_timeout_rports(appl_name) %}
{%- set connected_pport = rport["connected_ports"]["ports"][0] %}
{%- set arg_list = get_output_args_str_list(rport["operation"]["args"].values()) %}
{%- set arg_list_str = "void" if arg_list | length == 0 else arg_list | join(", ") %}
{%- if rport["return_mode"] == "waiting" %}
{#- API Implementation for CS async waiting mode #}
FUNC(Std_ReturnType,RTE_CODE) Rte_Result_{{rport["component"]}}_{{rport["name"]}}({{arg_list_str}})
{
    Std_ReturnType ret = RTE_E_OK;
    EventMaskType ev;
    ScheduleTableType schtbl = (ScheduleTableType){{rport["schedule_table"]["id"]}};
    uint8 clientId = Rte_CS_ClientConfigIndex_{{rport["component"]}}_{{rport["name"]}};

    const Rte_CS_DataType_{{connected_pport["component"]}}_{{connected_pport["name"]}} *clientData = &Rte_CS_Data_{{connected_pport["component"]}}_{{connected_pport["name"]}}[clientId];

    {%- set the_same_task = rport['task'][0].upper() %}
#if (RTE_DLOG_ENABLED == STD_ON)
    Std_ReturnType retLog = StartScheduleTableRel(schtbl, RTE_START_SCH_TBL_OFFSET);
    if (retLog != E_OK)
    {
        Rte_LogOs(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_StartScheduleTableRel, schtbl, retLog);
    }
    do
    {
        retLog = WaitEvent(((EventMaskType) RTE_EV_WAITPOINT_{{the_same_task}}) | ((EventMaskType) RTE_EV_TIMEOUT_{{the_same_task}}));
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_WaitEvent, RTE_DLOG_EMPTY_ID, retLog);
        }
        retLog = GetEvent((TaskType) OS_TASK_{{the_same_task}}_ID, &ev);
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_GetEvent, RTE_DLOG_EMPTY_ID, retLog);
        }
        retLog = ClearEvent(ev & (((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}}) | ((EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}})));
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_ClearEvent, RTE_DLOG_EMPTY_ID, retLog);
        }
        if ((ev & (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}}) == (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}})
        {
            ret = RTE_E_TIMEOUT;
        }
    }
    while((ret != RTE_E_TIMEOUT) && (*clientData->Rte_CallCompleted_Client != *clientData->Rte_CallCompleted_Server));

    if (ret != RTE_E_TIMEOUT)
    {
        StatusType osStatus = StopScheduleTable(schtbl);
        if (osStatus != E_OK)
        {
            Rte_LogOs(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_StopScheduleTable, schtbl, osStatus);
        }
    }
#else
    (void)StartScheduleTableRel(schtbl, RTE_START_SCH_TBL_OFFSET);
    do
    {
        (void)WaitEvent(((EventMaskType) RTE_EV_WAITPOINT_{{the_same_task}}) | ((EventMaskType) RTE_EV_TIMEOUT_{{the_same_task}}));
        (void)GetEvent((TaskType) OS_TASK_{{the_same_task}}_ID, &ev);
        (void)ClearEvent(ev & (((EventMaskType)RTE_EV_WAITPOINT_{{the_same_task}}) | ((EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}})));

        if ((ev & (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}}) == (EventMaskType)RTE_EV_TIMEOUT_{{the_same_task}})
        {
            ret = RTE_E_TIMEOUT;
        }
    }
    while((ret != RTE_E_TIMEOUT) && (*clientData->Rte_CallCompleted_Client != *clientData->Rte_CallCompleted_Server));
    if (ret != RTE_E_TIMEOUT)
    {
        (void)StopScheduleTable(schtbl);
    }
#endif

    if (ret != RTE_E_TIMEOUT)
    {
{{-rte_result_cp_result_out(rport, "        ")}}
    }
#if (RTE_DLOG_ENABLED == STD_ON)
    if (ret != RTE_E_OK)
    {
        Rte_LogRte(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, ret);
    }
#endif
    return ret;
}

{%- elif rport["return_mode"] == "polling" %}
    {%- set sch_tbl_id = rport["schedule_table"]["id"] %}
{#- API Implementation for CS async polling mode #}
FUNC(Std_ReturnType,RTE_CODE) Rte_Result_{{rport["component"]}}_{{rport["name"]}}({{arg_list_str}})
{
    Std_ReturnType ret = RTE_E_OK;
    volatile boolean callCompleted = (boolean)FALSE;
    uint8 clientId = Rte_CS_ClientConfigIndex_{{rport["component"]}}_{{rport["name"]}};

    ScheduleTableStatusType osStatus = SCHEDULETABLE_STOPPED;
    (void)GetScheduleTableStatus((ScheduleTableType){{sch_tbl_id}}, &osStatus);

    const Rte_CS_DataType_{{connected_pport["component"]}}_{{connected_pport["name"]}} *clientData = &Rte_CS_Data_{{connected_pport["component"]}}_{{connected_pport["name"]}}[clientId];
    callCompleted = (*clientData->Rte_CallCompleted_Client == *clientData->Rte_CallCompleted_Server);
    if (callCompleted)
    {
{{-rte_result_cp_result_out(rport, "        ")}}
    }
    else if (osStatus == SCHEDULETABLE_STOPPED) {
        ret = RTE_E_TIMEOUT;
    }
    else {
        ret = RTE_E_NO_DATA;
    }

    if (ret != RTE_E_TIMEOUT)
    {
#if (RTE_DLOG_ENABLED == STD_ON)
        Std_ReturnType retLog = StopScheduleTable((ScheduleTableType){{sch_tbl_id}});
        if (retLog != E_OK)
        {
            Rte_LogOs(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, OSServiceId_StopScheduleTable, {{sch_tbl_id}}, retLog);
        }
#else
        StopScheduleTable((ScheduleTableType){{sch_tbl_id}});
#endif
    }

#if (RTE_DLOG_ENABLED == STD_ON)
    if (ret != RTE_E_OK)
    {
        Rte_LogRte(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, ret);
    }
#endif
    return ret;
}
{%- endif %}
{% endfor %}

{%- for rport in get_appl_async_cs_multicore_without_timeout_rports(appl_name) %}
{%- set connected_pport = rport["connected_ports"]["ports"][0] %}
{%- set arg_list = get_output_args_str_list(rport["operation"]["args"].values()) %}
{%- set arg_list_str = "void" if arg_list | length == 0 else arg_list | join(", ") %}
{%- if rport["return_mode"] == "waiting" %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Result_{{rport["component"]}}_{{rport["name"]}}({{arg_list_str}})
{
    Std_ReturnType ret = RTE_E_OK;
    volatile boolean callCompleted = (boolean)FALSE;
    uint8 clientId = Rte_CS_ClientConfigIndex_{{rport["component"]}}_{{rport["name"]}};

    const Rte_CS_DataType_{{connected_pport["component"]}}_{{connected_pport["name"]}} *clientData = &Rte_CS_Data_{{connected_pport["component"]}}_{{connected_pport["name"]}}[clientId];
    do
    {
        callCompleted = (*clientData->Rte_CallCompleted_Client == *clientData->Rte_CallCompleted_Server);

        if (callCompleted)
        {
{{-rte_result_cp_result_out(rport, "            ")}}
        }
    }
    while(!callCompleted);
#if (RTE_DLOG_ENABLED == STD_ON)
    if (ret != RTE_E_OK)
    {
        Rte_LogRte(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, ret);
    }
#endif
    return ret;
}
{%- elif rport["return_mode"] == "polling" %}
FUNC(Std_ReturnType,RTE_CODE) Rte_Result_{{rport["component"]}}_{{rport["name"]}}({{arg_list_str}})
{
    Std_ReturnType ret = RTE_E_OK;
    volatile boolean callCompleted = (boolean)FALSE;
    uint8 clientId = Rte_CS_ClientConfigIndex_{{rport["component"]}}_{{rport["name"]}};

    const Rte_CS_DataType_{{connected_pport["component"]}}_{{connected_pport["name"]}} *clientData = &Rte_CS_Data_{{connected_pport["component"]}}_{{connected_pport["name"]}}[clientId];
    Rte_DisableOSInterrupts();
    callCompleted = (*clientData->Rte_CallCompleted_Client == *clientData->Rte_CallCompleted_Server);
    if (callCompleted)
    {
{{-rte_result_cp_result_out(rport, "        ")}}
    }
    else {
        ret = RTE_E_NO_DATA;
    }
    Rte_EnableOSInterrupts();

#if (RTE_DLOG_ENABLED == STD_ON)
    if (ret != RTE_E_OK)
    {
        Rte_LogRte(RTE_RESULT_SERVICE_ID, Rte_Result_{{rport["component"]}}_{{rport["name"]}}_ID, ret);
    }
#endif
    return ret;
}
{%- endif %}
{%- endfor %}

{#- RTE Non-IOC queue handling -#}
{%- set appl_queues = get_all_sr_queue_non_ioc_dequeue_dict_by_appl(appl_name) %}
{%- if appl_queues %}
/*
 * Queue handling functions
 */
    {%- set queues = appl_queues[appl_name] %}
FUNC(Rte_QReturnType, RTE_CODE) Rte_Enqueue_{{appl_name}}(Rte_QIndexType q, const void* data)
{
    Rte_QReturnType ret = RTE_Q_FULL;
    uint8 tail = 0U;
    Rte_QRamInfoType* qRam = &(Rte_{{appl_name}}_QRamInfo[q]);
    const Rte_QRomInfoType* qRom = &(Rte_{{appl_name}}_QRomInfo[q]);

    Rte_DisableOSInterrupts();
    if (qRam->Rte_ElementCnt < qRom->Rte_MaxElements)
    {
        tail = qRam->Rte_tail;
        qRam->Rte_tail = (qRam->Rte_tail + 1U) % (qRom->Rte_MaxElements);
        qRam->Rte_ElementCnt++;

        ret = RTE_Q_OK;
    }
    Rte_EnableOSInterrupts();

    if (ret == RTE_Q_OK) {
        Rte_MemCpy((void *)&((uint8 *)qRom->Rte_BasePtr)[tail * qRom->Rte_BytesPerElement], data, qRom->Rte_BytesPerElement);
    }
    return ret;
}

FUNC(Rte_QReturnType, RTE_CODE) Rte_Dequeue_{{appl_name}}(Rte_QIndexType q, void* data)
{
    Rte_QReturnType ret = RTE_Q_EMPTY;
    uint8 head = 0U;
    Rte_QRamInfoType* qRam = &(Rte_{{appl_name}}_QRamInfo[q]);
    const Rte_QRomInfoType* qRom = &(Rte_{{appl_name}}_QRomInfo[q]);

    Rte_DisableOSInterrupts();
    if (0U < qRam->Rte_ElementCnt)
    {
        head = qRam->Rte_head;
        qRam->Rte_head = (qRam->Rte_head + 1U) % (qRom->Rte_MaxElements);
        qRam->Rte_ElementCnt--;

        ret = RTE_Q_OK;
    }
    Rte_EnableOSInterrupts();

    if (ret != RTE_Q_EMPTY)
    {
        Rte_MemCpy(data, (void *)&((uint8 *)qRom->Rte_BasePtr)[head * qRom->Rte_BytesPerElement], qRom->Rte_BytesPerElement);
    }
    return ret;
}
{%- endif %}

{#- RTE IOC queue handling -#}
{%- set sr_ioc_enqueues = get_all_sr_queue_ioc_enqueue_dict_by_appl(appl_name) %}
{%- if sr_ioc_enqueues %}
    {%- for queue in sr_ioc_enqueues[appl_name] %}
    {%- set datatype = queue['datatype'] %}
    {%- set data_arg = get_sr_write_data_arg(datatype) %}
FUNC(Std_ReturnType, RTE_CODE) Rte_IocSend_{{queue['component']}}_{{queue['port']}}_{{queue['name']}}({{data_arg}})
{
    Std_ReturnType ret = RTE_E_OK;
    uint8 tail = 0U;

    {%- if queue['ioc_type'] == 'OSISR' %}
    Rte_DisableOSInterrupts();
    {%- elif queue['ioc_type'] == 'SPINLOCK' %}
    (void)GetSpinlock((SpinlockIdType)OS_SPINLOCK_RTE_{{queue["name"].upper()}}_ID);
    {%- endif %}
    if ({{queue['size']}}U <= Rte_ioc_{{queue['name']}}_ElementCnt)
    {
        Rte_ioc_{{queue['name']}}_Overflow0 = !Rte_ioc_{{queue['name']}}_Overflow1;
        ret = RTE_E_LIMIT;
    }
    else
    {
        tail = Rte_ioc_{{queue['name']}}_tail;
        Rte_ioc_{{queue['name']}}_tail = (Rte_ioc_{{queue['name']}}_tail + 1U) % {{queue['size']}}U;
        Rte_ioc_{{queue['name']}}_ElementCnt++;
    }
    {%- if queue['ioc_type'] == 'OSISR' %}
    Rte_EnableOSInterrupts();
    {%- elif queue['ioc_type'] == 'SPINLOCK' %}
    (void)ReleaseSpinlock((SpinlockIdType)OS_SPINLOCK_RTE_{{queue["name"].upper()}}_ID);
    {%- endif %}

    if (ret == RTE_E_OK) {
    {%- if datatype['native_category'] == 'NUMERICAL' %}
        Rte_ioc_{{queue['name']}}[tail] = data;
    {%- else %}
        Rte_MemCpy(&Rte_ioc_{{queue['name']}}[tail], data, sizeof({{datatype['name']}}));
    {%- endif %}
    }

    {#- TODO: SetEvent #}
    return ret;
}
    {%- endfor %}
{%- endif %}

{%- set appl_ioc_dequeues = get_all_sr_queue_ioc_dequeue_dict_by_appl(appl_name=appl_name, is_result_combined=True) %}
{%- if appl_ioc_dequeues %}
    {%- for queue in appl_ioc_dequeues[appl_name] %}
    {%- set datatype = queue['datatype'] %}
    {%- set data_arg = get_sr_read_data_arg(datatype) %}
FUNC(Std_ReturnType, RTE_CODE) Rte_IocReceive_{{queue['component']}}_{{queue['name']}}({{data_arg}})
{
    if (0U == Rte_ioc_{{queue['name']}}_ElementCnt)
    {
        return RTE_E_NO_DATA;
    }

    Std_ReturnType ret = RTE_E_OK;
    uint8 head;

    {%- if queue['ioc_type'] == 'OSISR' %}
    Rte_DisableOSInterrupts();
    {%- elif queue['ioc_type'] == 'SPINLOCK' %}
    (void)GetSpinlock((SpinlockIdType)OS_SPINLOCK_RTE_{{queue["name"].upper()}}_ID);
    {%- endif %}

    if (Rte_ioc_{{queue['name']}}_Overflow1 != Rte_ioc_{{queue['name']}}_Overflow0)
    {
        ret = RTE_E_LOST_DATA;
        Rte_ioc_{{queue['name']}}_Overflow1 = Rte_ioc_{{queue['name']}}_Overflow0;
    }
    head = Rte_ioc_{{queue['name']}}_head;
    Rte_ioc_{{queue['name']}}_head = (Rte_ioc_{{queue['name']}}_head + 1U) % {{queue['size']}}U;
    Rte_ioc_{{queue['name']}}_ElementCnt--;

    {%- if queue['ioc_type'] == 'OSISR' %}
    Rte_EnableOSInterrupts();
    {%- elif queue['ioc_type'] == 'SPINLOCK' %}
    (void)ReleaseSpinlock((SpinlockIdType)OS_SPINLOCK_RTE_{{queue["name"].upper()}}_ID);
    {%- endif %}

    {%- if datatype['native_category'] == 'NUMERICAL' %}
    *data = Rte_ioc_{{queue['name']}}[head];
    {%- else %}
    Rte_MemCpy(data, &Rte_ioc_{{queue['name']}}[head], sizeof({{datatype['name']}}));
    {%- endif %}

    return ret;
}
    {%- endfor %}
{%- endif %}

{%- set cs_ioc_enqueues = get_all_cs_ioc_enqueue_dict_by_appl(appl_name=appl_name, is_result_combined=True) %}
{%- if cs_ioc_enqueues %}
{%- for queue in cs_ioc_enqueues[appl_name] %}
FUNC(Std_ReturnType, RTE_CODE) Rte_IocSend_{{queue['component']}}_{{queue['client_name']}}(uint8 data)
{
    Std_ReturnType ret = RTE_E_OK;
    uint8 tail = 0U;

    {%- if queue['ioc_type'] == 'OSISR' %}
    Rte_DisableOSInterrupts();
    {%- elif queue['ioc_type'] == 'SPINLOCK' %}
    (void)GetSpinlock((SpinlockIdType)OS_SPINLOCK_RTE_{{queue["name"].upper()}}_ID);
    {%- endif %}
    if ({{queue['size']}}U <= Rte_ioc_{{queue['name']}}_ElementCnt)
    {
        Rte_ioc_{{queue['name']}}_Overflow0 = !Rte_ioc_{{queue['name']}}_Overflow1;
        ret = RTE_E_LIMIT;
    }
    else
    {
        tail = Rte_ioc_{{queue['name']}}_tail;
        Rte_ioc_{{queue['name']}}_tail = (Rte_ioc_{{queue['name']}}_tail + 1U) % {{queue['size']}}U;
        Rte_ioc_{{queue['name']}}_ElementCnt++;
    }
    {%- if queue['ioc_type'] == 'OSISR' %}
    Rte_EnableOSInterrupts();
    {%- elif queue['ioc_type'] == 'SPINLOCK' %}
    (void)ReleaseSpinlock((SpinlockIdType)OS_SPINLOCK_RTE_{{queue["name"].upper()}}_ID);
    {%- endif %}

    if (ret == RTE_E_OK) {
        Rte_ioc_{{queue['name']}}[tail] = data;
    }

    return ret;
}
{%- endfor %}
{%- endif %}

{%- set cs_ioc_dequeues = get_all_cs_ioc_dequeue_dict_by_appl(appl_name=appl_name, is_result_combined=True) %}
{%- if cs_ioc_dequeues %}
{%- for queue in cs_ioc_dequeues[appl_name] %}
FUNC(Std_ReturnType, RTE_CODE) Rte_IocReceive_{{queue['component']}}_{{queue['name']}}(uint8 *data)
{
    if (0U == Rte_ioc_{{queue['name']}}_ElementCnt)
    {
        return RTE_E_NO_DATA;
    }

    Std_ReturnType ret = RTE_E_OK;
    uint8 head;

    {%- if queue['ioc_type'] == 'OSISR' %}
    Rte_DisableOSInterrupts();
    {%- elif queue['ioc_type'] == 'SPINLOCK' %}
    (void)GetSpinlock((SpinlockIdType)OS_SPINLOCK_RTE_{{queue["name"].upper()}}_ID);
    {%- endif %}

    if (Rte_ioc_{{queue['name']}}_Overflow1 != Rte_ioc_{{queue['name']}}_Overflow0)
    {
        ret = RTE_E_LOST_DATA;
        Rte_ioc_{{queue['name']}}_Overflow1 = Rte_ioc_{{queue['name']}}_Overflow0;
    }
    head = Rte_ioc_{{queue['name']}}_head;
    Rte_ioc_{{queue['name']}}_head = (Rte_ioc_{{queue['name']}}_head + 1U) % {{queue['size']}}U;
    Rte_ioc_{{queue['name']}}_ElementCnt--;

    {%- if queue['ioc_type'] == 'OSISR' %}
    Rte_EnableOSInterrupts();
    {%- elif queue['ioc_type'] == 'SPINLOCK' %}
    (void)ReleaseSpinlock((SpinlockIdType)OS_SPINLOCK_RTE_{{queue["name"].upper()}}_ID);
    {%- endif %}

    *data = Rte_ioc_{{queue['name']}}[head];
    return ret;
}
{%- endfor %}
{%- endif %}

{#- ---------------------------------------------------------------- #}
{#- Inter-runnable triggering (Rte_IrTrigger API -- Internal Trigger) #}
{#- ---------------------------------------------------------------- #}
{%- set int_trigger_apis = get_standard_internal_trigger_api() %}
{%- if int_trigger_apis %}
/*
 * Inter-runnable triggering (Rte_IrTrigger API)
 */
{%- endif %}
{%- for swc_runnable_trigger, trigger_apis in int_trigger_apis.items() %}
{%- set swc, runnable, trigger = swc_runnable_trigger.split('/') %}
FUNC(void, RTE_CODE) Rte_IrTrigger_{{swc}}_{{runnable}}_{{trigger}}(void)
{
    {%- for trigger_api in trigger_apis %}
        {%- if trigger_api["task_id"] and trigger_api["event_mask"] %}
    /* scheduled trigger for runnables: {{trigger_api['triggered_runnable']}} */
    (void)SetEvent((TaskType) {{trigger_api["task_id"].upper()}}, (EventMaskType){{trigger_api["event_mask"].upper()}});
        {%- endif %}
    {%- endfor %}
}
{%- endfor %}

{#- ---------------------------------------------------------------- #}
{#- Rte Trigger API (Rte_Trigger -- External Trigger) #}
{#- ---------------------------------------------------------------- #}
{%- set ext_trigger_apis = get_external_trigger_api() %}
{%- if ext_trigger_apis %}
/*
 * Rte Trigger API (Rte_Trigger)
 */
{%- endif %}
{%- for trigger_api in ext_trigger_apis %}
FUNC(void, RTE_CODE) Rte_Trigger_{{trigger_api["sort_name"]}}(void)
{
    {%- if trigger_api["set_events"] %}
        {%- for value in trigger_api["set_events"] %}
    /* scheduled trigger for runnables: {{trigger_api["connected_ports"]["ports"][loop.index0]["symbol"]}} */
    (void)SetEvent((TaskType) {{value["task_id"].upper()}}, (EventMaskType){{value["event_mask"].upper()}});
        {%- endfor %}
    {%- endif %}
}
{%- endfor %}

{#- ---------------------------------------------------------------- #}
{#- IRV internal func #}
{#- ---------------------------------------------------------------- #}

{%- for api in get_osa_irv_exp_write_api_func(appl_name) %}
    {%- set data_arg = get_sr_write_data_arg(api["data"]["datatype"], 'irv') %}
FUNC(void, RTE_CODE) Rte_IrvWrite_{{api["swc_inst"]}}_{{api["run_var"]}}({{data_arg}})
{
    {%- if api["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
    {%- endif %}

    {%- if api["data"]["datatype"]["native_category"] == "NUMERICAL" %}
    {{api["data"]["buff"]}} = data;
    {%- else %}
    Rte_MemCpy(&{{api["data"]["buff"]}}, data, sizeof({{api["data"]["datatype"]["name"]}}));
    {%- endif %}

    {%- if api["lock"] == "OSISR" %}
    Rte_EnableOSInterrupts();
    {%- endif %}
}
{%- endfor %}

{%- for api in get_osa_irv_exp_read_api_func(appl_name) %}
    {%- set datatype = api["data"]["datatype"]["name"] %}
    {%- set data_arg = get_sr_read_data_arg(datatype, 'irv') %}
    {%- if api["data"]["datatype"]["native_category"] == "NUMERICAL" %}
FUNC({{datatype}}, RTE_CODE) Rte_IrvRead_{{api["swc_inst"]}}_{{api["run_var"]}}({{data_arg}})
{
        {%- if api["lock"] == "OSISR" %}
    {{datatype}} local;
    Rte_DisableOSInterrupts();
    local = {{api["data"]["buff"]}};
    Rte_EnableOSInterrupts();
    return local;
        {%- endif %}
}
    {%- else %}
FUNC(void, RTE_CODE) Rte_IrvRead_{{api["swc_inst"]}}_{{api["run_var"]}}({{data_arg}})
{
        {%- if api["lock"] == "OSISR" %}
    Rte_DisableOSInterrupts();
    Rte_MemCpy(data, &{{api["data"]["buff"]}}, sizeof({{api["data"]["datatype"]["name"]}}));
    Rte_EnableOSInterrupts();
        {%- endif %}
}
    {%- endif %}
{%- endfor %}


{%- macro invoke_com_send_in_rte_com_proxy(signal_name, signal, buff_prefix) %}
{%- set buff = buff_prefix + signal["data_mapping_buff"]["name"] %}
{%- set type_mappings = signal["type_mappings"].get(signal_name) %}
{%- if signal["datatype"]["native_category"] == "STRUCTURE" %}
    {%- for type_mapping in type_mappings %}
        {%- if gen_compu_method %}
        {{-macros.gen_tx_signal_group_dm_compu_method_proxy("        ", buff, buff, type_mapping["implementation_record_element"], type_mapping["real_sig_type"], type_mapping["compu_method"])}}
        {%- endif %}
        {%- if loop.first %}
            {%- if gen_compu_method %}
        ret = com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &(Local_{{buff}}_{{type_mapping["implementation_record_element"]}}));
            {%- else %}
        ret = com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &({{buff}}.{{type_mapping["implementation_record_element"]}}));
            {%- endif %}
        {%- else %}
            {%- if gen_compu_method %}
        ret |= com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &(Local_{{buff}}_{{type_mapping["implementation_record_element"]}}));
            {%- else %}
        ret |= com_send_signal(ComConf_ComGroupSignal_{{type_mapping["i_signal"]}}_Tx, &({{buff}}.{{type_mapping["implementation_record_element"]}}));
            {%- endif %}
        {%- endif %}
    {%- endfor %}
        ret |= com_send_signal_group(ComConf_ComSignalGroup_{{signal_name}}_Tx);
{%- elif signal["datatype"]["native_category"] == "ARRAY" %}
        ret = com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, *(&{{buff}}));
{%- else %}
        {%- if gen_compu_method and signal["datatype"]["native_category"] == "NUMERICAL" %}
        {{-macros.gen_tx_num_sig_dm_compu_method_proxy("        ", buff, buff, signal["real_sig_type"], signal['compu_method'], 'false')}}
        ret = com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, (&Local_{{buff}}));
        {%- else %}
        ret = com_send_signal(ComConf_ComSignal_{{signal_name}}_Tx, (&{{buff}}));
        {%- endif %}
{%- endif %}
{%- if signal["data_mapping_status"] %}
        {{signal["data_mapping_status"]["name"]}} = ret;
{%- endif %}
{%- if signal["is_com_tx_opt"] %}
    {%- set is_proxy_sanding = signal["data_mapping_flag_is_proxy_sending"]["parent_struct_name"] + "." + signal["data_mapping_flag_is_proxy_sending"]["name"] %}
        if (ret != E_OK)
        {
            {{is_proxy_sanding}} = 1U;
        }
{%- endif %}
{%- endmacro %}

{%- macro dlog_in_rte_com_proxy(signal_name, signal_info, api_id) %}
#if (RTE_DLOG_ENABLED == STD_ON)
        if (ret != E_OK)
        {
{%- if signal_info["datatype"]["native_category"] == "STRUCTURE" %}
            Rte_LogCom(RTE_COMSENDSIGNALPROXYPERIODIC_SERVICE_ID, {{api_id}}, RTE_COM_SENDSIGNALGROUP_ID, ComConf_ComSignalGroup_{{signal_name}}_Tx, ret);
{%- else %}
            Rte_LogCom(RTE_COMSENDSIGNALPROXYPERIODIC_SERVICE_ID, {{api_id}}, RTE_COM_SENDSIGNAL_ID, ComConf_ComSignal_{{signal_name}}_Tx, ret);
{%- endif %}
        }
#endif
{%- endmacro %}

/*
 * RTE Schedulable entity for COM-Access from different partitions
 */
{%- for proxy_name in get_rte_com_tx_proxy_list(osa=appl_name) %}
{%- set data_dict_same_core, data_dict_inter_core, local_data_dict_inter_core, is_updated_tx_parent_struct = get_rte_com_tx_proxy_data_dict(proxy_name) %}
FUNC(void, RTE_CODE) {{proxy_name}}(void)
{
{%- if data_dict_same_core or data_dict_inter_core %}
    Std_ReturnType ret = E_OK;
    {%- if data_dict_same_core %}
    /**
     * inter partition same core
    */
    {%- endif %}
{%- endif %}

{#- inter partition same core proxy #}
{%- for signal_name, signal_info in data_dict_same_core.items() %}
    if ({{signal_info["data_mapping_flag"]["is_updated"]["parent_struct_name"]}}.{{signal_info["data_mapping_flag"]["is_updated"]["name"]}} == 1U)
    {
    {%- if signal_info["data_mapping_lock"] == "OSISR" %}
        Rte_DisableOSInterruptsOS();
    {%- endif %}
        {{-invoke_com_send_in_rte_com_proxy(signal_name, signal_info, "")}}
        {{signal_info["data_mapping_flag"]["is_updated"]["parent_struct_name"]}}.{{signal_info["data_mapping_flag"]["is_updated"]["name"]}} = 0U;
    {%- if signal_info["data_mapping_lock"] == "OSISR" %}
        Rte_EnableOSInterruptsOS();
    {%- endif %}
        {{-dlog_in_rte_com_proxy(signal_name, signal_info, proxy_name + "_ID")}}
    }
{%- endfor %}

{#- inter core temp is_updated flag #}
{%- if local_data_dict_inter_core %}
    /**
     * inter core
    */
    {%- for is_update_struct in is_updated_tx_parent_struct %}
    {{is_update_struct}}_Type local_{{is_update_struct}} = {0U};
    {%- endfor %}
    {%- for _, data_dict in local_data_dict_inter_core.items() %}
        {%- for _, signal_info in data_dict.items() %}
            {%- if not signal_info["is_inter_core_extra_copy_skipped"] %}
    {{signal_info["datatype"]["name"]}} local_{{signal_info["data_mapping_buff"]["name"]}};
            {%- endif %}
        {%- endfor %}
    {%- endfor %}
{%- endif %}

{#- inter core data exchange #}
{%- for spinlock_id, data_dict in local_data_dict_inter_core.items() %}
    {%- set spinlock_postfix = "Read" if spinlock_id.endswith("_RW") else "" %}
    (void) GetSpinlock{{spinlock_postfix}}((SpinlockIdType) {{spinlock_id}});
    {%- for signal_name, signal_info in data_dict.items() %}
        {%- set buff = signal_info["data_mapping_buff"] %}
        {%- set datatype = signal_info["datatype"] %}
        {%- set is_updated_flag = signal_info["data_mapping_flag"]["is_updated"] %}
    if ({{is_updated_flag["parent_struct_name"]}}.{{is_updated_flag["name"]}} == 1U)
    {
        {%- if not signal_info["is_inter_core_extra_copy_skipped"] %}
            {%- if datatype["native_category"] in ["NUMERICAL", "VALUE"] %}
        local_{{buff["name"]}} = {{buff["name"]}};
            {%- else %}
        Rte_MemCpy(&local_{{buff["name"]}}, &{{buff["name"]}}, sizeof(local_{{buff["name"]}}));
            {%- endif %}
        {%- endif %}
        local_{{is_updated_flag["parent_struct_name"]}}.{{is_updated_flag["name"]}} = {{is_updated_flag["parent_struct_name"]}}.{{is_updated_flag["name"]}};
        {{is_updated_flag["parent_struct_name"]}}.{{is_updated_flag["name"]}} = 0U;
    }
    {%- endfor %}
    (void) ReleaseSpinlock{{spinlock_postfix}}((SpinlockIdType) {{spinlock_id}});
{%- endfor %}

{#- inter core send to com #}
{%- for signal_name, signal_info in data_dict_inter_core.items() %}
    {%- set is_updated_flag = signal_info["data_mapping_flag"]["is_updated"] %}
    if (local_{{is_updated_flag["parent_struct_name"]}}.{{is_updated_flag["name"]}} == 1U)
    {
        {{-invoke_com_send_in_rte_com_proxy(signal_name, signal_info, "local_")}}
        {{-dlog_in_rte_com_proxy(signal_name, signal_info, proxy_name + "_ID")}}
    }
{%- endfor %}
}
{%- endfor %}

#define RTE_STOP_SEC_CODE
#include "rte_memmap.h"
{# blank line at end of file #}
